What is the fundamental difference between the Set<E>
and List<E>
interfaces?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
This might not be the answer you're looking for, but the JavaDoc of the collections classes is actually pretty descriptive. Copy/pasted:
TOPIC Name: List VS Set
I have just gone through Java's most important topic called Collections Framework. I thought to share my little knowledge about Collections with you. List, Set, Map are the most important topic of it. So let's start with List and Set.
Difference between List and Set:
List is a collection class which extends
AbstractList
class where as Set is a collection class which extendsAbstractSet
class but both implements Collection interface.List interface allows duplicate values (elements) whereas Set interface does not allow duplicate values. In case of duplicate elements in Set, it replaces older values.
List interface allows NULL values where as Set interface does not allow Null values. In case of using Null values in Set it gives
NullPointerException
.List interface maintains insertion order. That means the way we add the elements in the List in the same way we obtain it using iterator or for-each style. Whereas
Set
implementations do not necessarily maintain insertion order. (AlthoughSortedSet
does usingTreeSet
, andLinkedHashSet
maintains insertion order).List interface has its own methods defined whereas Set interface does not have its own method so Set uses Collection interface methods only.
List interface has one legacy class called
Vector
whereas Set interface does not have any legacy classLast but not the least... The
listIterator()
method can only be used to cycle through the elements within List Classes whereas we can use iterator() method to access Set class elementsAnything else can we add? Please let me know.
Thanks.
A set is an unordered group of distinct objects — no duplicate objects are allowed. It is generally implemented using the hash code of the objects being inserted. (Specific implementations may add ordering, but the Set interface itself does not.)
A list is an ordered group of objects which may contain duplicates. It could be implemented with an
ArrayList
,LinkedList
, etc.Hi So many answers are already given..Let me point out some points which are not mentioned so far:
RandomAccess
interface which is a marker interface for faster access. None of the List implementations do that.ListIterator
which supports iteration in both directions. Set uses Iterator which supports only 1 way iterationSet: A Set cannot have Duplicate elements in its collections. it is also an unordered collection. To access the data from Set, it is required to use Iterator only and index based retrieve is not possible for it. It is mainly used whenever required uniqueness collection.
List: A List can have duplicate elements, with the natural ordered as it is inserted. Thus, it can be retrieved data based on index or iterator. It is widely used to store collection which needs to access based on index.