java LinkedHashSet 或 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3538059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:17:59  来源:igfitidea点击:

LinkedHashSet or ArrayList

javadata-structures

提问by Cheok Yan Cheng

I wish to

我希望

  1. Avoid duplicated item being inserted.
  2. When I iterate through the collection class, the returned item is same as insertion order.
  1. 避免插入重复的项目。
  2. 当我遍历集合类时,返回的项目与插入顺序相同。

May I know, what thing I should consider, to choose either ArrayList (explicitly perform contains check before insertion) or LinkedHashSet?

我可以知道,我应该考虑什么,选择 ArrayList(在插入前显式执行包含检查)还是 LinkedHashSet?

Thanks.

谢谢。

回答by Klark

Definitely use LinkedHashSet. It is made for what you need. Searching entire ArrayList every time you need to insert something will be performance killer (O(n) every time))

绝对使用 LinkedHashSet。它专为您的需要而设计。每次需要插入内容时都搜索整个 ArrayList 将是性能杀手(每次都是 O(n)))

回答by True Soft

Use LinkedHashSetif you don't want duplicate items inserted.

LinkedHashSet如果您不想插入重复的项目,请使用。

回答by JRL

A LinkedHashSetseems to fit the bill perfectly.

ALinkedHashSet似乎完全符合要求。

When you build your own objects, and plan to use them in a Collection like LinkedHashSethere. Don't forget to override both equalsand hashcodefor the item you are going to store in it.

当您构建自己的对象并计划在像LinkedHashSet这里这样的集合中使用它们时。不要忘记覆盖两者equals以及hashcode您要存储在其中的项目。

回答by Karoly

Please check this out: http://wiki3.cosc.canterbury.ac.nz/images/e/e9/JavaCollections.png

请查看:http: //wiki3.cosc.canterbury.ac.nz/images/e/e9/JavaCollections.png

LinkedHashSet is what you need, because it's an implementation of the Setinterface. Set has one very cool habit: it doesn't allows duplicates by default. So, we are done with your 1.

LinkedHashSet 正是您所需要的,因为它是Set接口的实现。Set 有一个很酷的习惯:默认情况下它不允许重复。所以,我们完成了你的 1。

What about 2? We know, that we need one of the Set implementation, but which ?

2呢?我们知道,我们需要一个 Set 实现,但是哪个?

HashMap- you are able to store K,V pairs, but there is no order.

HashMap- 您可以存储 K,V 对,但没有顺序。

TreeSet- this is the slowest solution, because it's using a compareTomethod to keep every item sorted and ordered. This is why you can pass a comparator to it, when you are constructing a TreeSet.

TreeSet- 这是最慢的解决方案,因为它使用一种compareTo方法来保持每个项目的排序和排序。这就是为什么在构建 TreeSet 时可以将比较器传递给它的原因。

LinkedHashSet- Gives back the elements in order of INSERTING them. It is the ordered version of a HashSet.

LinkedHashSet- 按照插入的顺序返回元素。它是 HashSet 的有序版本。

Please find a cool description here: http://java67.blogspot.co.uk/2014/01/when-to-use-linkedhashset-vs-treeset-vs-hashset-java.html?_sm_au_=iVVMtMLHSDQ5P0P7

请在这里找到一个很酷的描述:http: //java67.blogspot.co.uk/2014/01/when-to-use-linkedhashset-vs-treeset-vs-hashset-java.html?_sm_au_=iVVMtMLHSDQ5P0P7