Java collection architecture
- Collection interface: This interface is the most basic Collection interface
- List Interface: List
- Set interface: A collection of mathematical concepts
- Map interface: contains key-value pairs. The Map cannot contain duplicate keys. SortedMap is a collection of maps sorted in ascending order.
Simplified diagram:
Note: There are several explanations for the above frame diagram
- All collection classes are located under the java.util package. Java’s Collection classes are derived primarily from two interfaces: Collection and Map, which are the root interfaces of the Java Collection framework, and which in turn contain subinterfaces or implementation classes.
- Collection Interface: Six interfaces (dotted lines) representing different collection types are the basis of the collection framework.
- Abstract Classes: Five abstract classes (long dashed lines) that partially implement the collection interface. Extensible to custom collection classes.
- Implementation classes: Eight implementation classes (represented by solid lines) that provide concrete implementations of interfaces.
- The Collection interface is a set of objects that allow repetition.
- The Set interface inherits Collection without repeating the Collection elements.
- The List interface inherits Collection, allowing repetition, and maintaining element insertion order.
- The Map interface is a key-value object and has nothing to do with the Collection interface.
- Set, List, and Map can be considered as three categories of sets: List sets are ordered sets, the elements of which can be repeated, and the elements of which can be accessed according to the index of the element. A Set is an unordered Set. Elements in a Set cannot be repeated. Elements in a Set can only be accessed based on the elements themselves (which is why elements in a Set are not allowed to be repeated). A Map contains key-value pairs of elements. Only the value of each element can be accessed based on its Key.
The standard collection classes are summarized in the following table:
class | Class description |
---|---|
AbstractCollection | Implements most of the collection interfaces. |
AbstractList | AbstractCollection inherits and implements most of the List interface. |
AbstractSequentialList | AbstractList descends from AbstractList to provide chained access to data elements instead of random access. |
LinkedList | AbstractSequentialList, implements a linked list. |
ArrayList | Implement dynamic arrays by inheriting AbstractList. |
AbstractSet | AbstractCollection inherits and implements most of the Set interface. |
HashSet | AbstractSet is inherited and uses a hash table. |
LinkedHashSet | Hash table and linked list implementation of Set interface with predictable iteration order. |
TreeSet | AbstractSet inherits from AbstractSet to sort elements using their natural order. |
AbstractMap | Implements most of the Map interface. |
HashMap | Inherits HashMap and uses a hash table. |
TreeMap | AbstractMap is inherited and uses a tree. |
WeakHashMap | AbstractMap class, using a weak key hash table. |
LinkedHashMap | Inherited from HashMap, elements are sorted using their natural order. |
IdentityHashMap | Inherit AbstractMap class to compare documents using reference equality. |