Overall, List,set,map is the interface that implements the collection interface.
- Repeatability:
- The list allows duplicate elements, which means that the list can insert new repeating elements without affecting the values of existing elements and their indexes.
- Set does not allow duplicate elements, and the set collection does not allow the insertion of duplicate elements.
- Map is a collection of mapping types that store elements in key-value pairs, where key (key) is unique and value (value) is repeatable.
- Null value:
- The list allows any number of null values.
- Set allows a maximum of one null value.
- Map allows only one null key to appear, but allows any number of null values.
- Sorting:
- The list maintains the order in which each element is inserted.
- The elements in the set are unordered, but some of the set's implementation classes can sort the elements in some order. (Ps:linkedhashset are sorted by the insertion order of the elements.) )
- Map, like set, wakes the elements out of order, but some of the map implementation classes also sort the elements. (Ps:treemap sorts the elements in ascending order by key, and Linkedhashmap sorts them according to the insertion sequence of each element)
- Implementation class:
- List:arraylist, LinkedList and so on.
- Set:hashset, Linkedhashset, TreeSet, SortedSet and so on.
- Map:hashmap, TreeMap, Weakhashmap, Linkedmashmap, Identityhashmap and so on.
- Usage scenarios:
- List (ArrayList) is a good choice if you need to always query based on the index value of an element.
- If your data does not allow duplicate values to exist, set is the more appropriate choice.
- If you need to preserve the insertion order of each element, then the list collection is preferred.
- If your data needs to be key-value mapped, then use map.
Original reference: Java tutorial
The difference between a list in Java and a collection