In Java's java.util package, there is a batch of classes called collection classes
The collection class, as the name implies, is the class that holds the collection of objects, which can be "ArrayList" in array form or "LinkedList" in the form of a list, or it can be combined with "HashMap" in two forms.
The parent class of the Java collection class is the collection interface, which is divided into three major categories: List, Map, Set.
Among them, list is a set of elements, the most commonly used for ArrayList, LinkedList;
Map is a set of object "key-value pairs", according to key to access value, most commonly used for hashmap and LinkedList;
Set is a true set of elements in a mathematical sense, which does not contain duplicate elements, most commonly used for hashset and TreeSet.
In the JDK source code, the underlying implementation of set is implemented using the corresponding map, the elements that are accessed in set as the key of the map, using the same object as value. For example, in HashSet, a hashmap is used as an internal storage implementation,
TreeSet is also the case, the internal use of a treemap to store elements, the picture is not affixed.
The difference between ArrayList and LinkedList has been written in front of the blog.
The difference between HashMap and TreeMap:
HashMap is based on the hash list implementation, the internal storage structure is adjacency table "array + linked list";
TreeMap is based on red-black trees, and the internal storage structure is red-black.
Therefore, key-value pairs that are deposited into the hashmap require that key be provided with the Hashcode () method as the hash basis, and this method is already available in the object class;
For TreeMap, either provide a comparator as the comparison key size when initializing TreeMap, or the class that requires key must implement comparable. If neither of these conditions is met, it will be reported as an exception when put in the TreeMap.
collection classes for Java