The Java Collection Toolkit, located under the Java.util package, contains a number of common data structures, such as arrays, lists, stacks, queues, collections, hash tables, and so on. The Learning Java Collection Framework can be broadly divided into the following five parts: list lists, set sets, map mappings, iterators (Iterator, enumeration), tool classes (Arrays, collections).
It can be seen that the collection class is divided into two main categories: collection and map.
Collection is a set of list, set and other highly abstracted interface, it contains the basic operation of these sets, it is mainly divided into two parts: list and set.
The list interface usually represents a listing (array, queue, linked list, stack, etc.), where elements can be duplicated, commonly implemented classes are ArrayList and LinkedList, and other vectors are not commonly used. In addition, LinkedList implements the queue interface, so it can also be used as a queue.
The set interface typically represents a collection where elements are not allowed to be duplicated (guaranteed by hashcode and the Equals function), and the common implementation classes have HashSet and Treeset,hashset are implemented through HASHMAP in the map. The TreeSet is implemented through the TREEMAP in the map. In addition, TreeSet implements the SortedSet interface, so it is an ordered set (the elements in the collection implement the comparable interface and overwrite the Compartor function).
We see that abstract classes Abstractcollection, Abstractlist, and Abstractset respectively implement the collection, list, and set interfaces, which are the many adapter design patterns used in the Java collection framework, Using these abstract classes to implement interfaces, implement some or all of the interfaces in an abstract class, so that some of the following classes simply inherit the abstract class directly and implement the method they need without implementing all the abstract methods in the interface.
Map is a mapping interface where each element is a Key-value key-value pair, and the same abstract class Abstractmap implements most of the functions in the map interface through the adapter pattern, TreeMap, HashMap, Weakhashmap and other implementation classes are implemented by inheriting Abstractmap, in addition, the less commonly used Hashtable directly implement the map interface, which and vector are JDK1.0 introduced in the collection class.
Iterator is an iterator that iterates through a collection (it cannot traverse the map, only to traverse collection), and collection implements the iterator () function, which returns a iterator object that iterates through the collection. Listiterator is designed to traverse the list. And enumeration is JDK1.0 when introduced, the role and iterator the same, but its function is less than iterator, it can only be used in Hashtable, vector and stack.
Arrays and collections are the two tool classes used to manipulate arrays and collections, such as a large number of calls to the Arrays.copyof () method in ArrayList and vectors. There are many static methods in collections that can return the synchronized version of each collection class, that is, the thread-safe version, and of course, if you want to use a thread-safe binding class, the corresponding collection class under the concurrent concurrency package is preferred.
--most of the above content is organized from the network
Java Collection source code analysis (a)