The Java Collection Toolkit is located under the Java.util package and contains many commonly used data structures, such as arrays, linked lists, stacks, queues, collections, hash tables, etc. Under the framework of learning Java collections, it can be roughly divided into the following five parts: List list, Set collection, Map map, iterator (Iterator, Enumeration), and tool classes (Arrays, Collections).
As can be seen from the above figure, collection classes are mainly divided into two categories: Collection and Map.
Collection is a highly abstract interface for collections such as List and Set. It contains the basic operations of these collections. It is mainly divided into two parts: List and Set.
The List interface usually represents a list (array, queue, linked list, stack, etc.), and the elements in it can be repeated. Common implementation classes are ArrayList and LinkedList, and there are also vectors that are not commonly used. In addition, LinkedList still implements the Queue interface, so it can also be used as a queue.
The Set interface usually represents a collection in which elements are not allowed to be repeated (guaranted by hashcode and equals functions). Commonly used implementation classes include HashSet and TreeSet. HashSet is implemented through HashMap in Map, and TreeSet is implemented through TreeMap in Map. In addition, TreeSet also implements the SortedSet interface, so it is an ordered set (elements in the set must implement the Comparable interface and overwrite the Compartor function).
We see that the abstract classes AbstractCollection, AbstractList and AbstractSet implement the Collection, List and Set interfaces respectively. This is the many adapter design patterns used in the Java collection framework. These abstract classes are used to implement interfaces and implement several or all methods in the interface in the abstract class. In this way, some of the following classes only need to directly inherit the abstract class and implement the methods they need, without implementing all abstract methods in the interface.
Map is a mapping interface, and each element in it is a key-value key-value pair. Similarly, the abstract class AbstractMap implements most functions in the Map interface through the adapter mode. TreeMap, HashMap, WeakHashMap and other implementation classes are implemented by inheriting AbstractMap. In addition, the uncommonly used HashTable directly implements the Map interface, and it and Vector are collection classes introduced in JDK1.0.
Iterator is an iterator that traverses collections (cannot traverse Map, only used to traverse Collections). The implementation classes of Collection implement the iterator() function, which returns an Iterator object to traverse collections, and ListIterator is specifically used to traverse Lists. Enumeration was introduced in JDK 1.0 and has the same function as Iterator, but its functions are less than Iterator, and it can only be used in Hashtable, Vector and Stack.
Arrays and Collections are two tool classes used to manipulate arrays and collections. For example, Arrays.Copyof() method is called in large quantities in ArrayList and Vector. There are many static methods in Collections that can return the synchronized version of each collection class, that is, the thread-safe version. Of course, if you want to use a thread-safe combination class, the corresponding collection class under Concurrent concurrent package is preferred.
The above comprehensive analysis of the source code of Java collection is all the content shared by the editor. I hope it can give you a reference and I hope you can support Wulin.com more.