Set<E>接口和List<E>接口的根本区别是什么?
当前回答
订购…列表有顺序,集合没有。
其他回答
这可能不是您想要的答案,但是集合类的JavaDoc实际上非常具有描述性。复制/粘贴:
An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list. Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements e1 and e2 such that e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.
嗨,已经给出了这么多答案,让我指出一些到目前为止没有提到的地方:
大多数的List实现(ArrayList,Vector)实现了RandomAccess接口,这是一个快速访问的标记接口。Set的实现都没有这样做。 List使用一个特殊的迭代器,称为ListIterator,它支持双向迭代。Set使用只支持单向迭代的迭代器 HashSet占用的内存是ArrayList的5.5倍 相同数量的元素。
1.List允许重复值,set不允许重复值
2.List维护您在列表中插入元素的顺序 Set不能维持秩序。 3.List是一个有序的元素序列,而Set是一个无序的元素列表。
List Vs Set
1) Set不允许重复。列表允许重复。基于Set的实现,它还维护插入顺序。
LinkedHashSet。它维护插入顺序。请参考此处
2)包含方法。根据集合的性质,它将为访问提供更好的性能。最好的情况是o(1)但是List有调用contains的性能问题。
元素的有序列表(是否唯一) 遵循Java的名为List的接口 可以通过索引访问吗
实现使用
LinkedList ArrayList
独特元素列表: 遵循Java名为Set的接口 不能通过索引访问
实现使用
HashSet(无序) LinkedHashSet(命令) 树集(按自然顺序或按提供的比较器排序)
接口Set和List都符合Java的名为Collection的接口
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 为什么元组可以包含可变项?
- 在Jar文件中运行类
- 带参数的可运行?
- 不区分大小写的“in”
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?