The collection hierarchy in Java consists of grouping elements/objects, where each class has subclasses and methods. It does not accept primitive types, but the "array" class allows the inclusion of several homogeneous elements of the same type, accepting primitive types.
The collections framework methods are present in the java.util package within the JDK (Java Development Kit). The main interfaces are List, Set and Map.
Generics
Use the symbol <> (diamond) for generic types. The most common type parameters include E (Element), K (Key), N (Number), T (Type), V (Value).
Comparator x Comparable
They are used for ordering collections. Comparable provides a single ordering sequence, affecting the original class, while Comparator provides multiple sequences without modifying the original class.
List x Set x Map
Examples of implementations/Classes:
Observations:
The first element added to a Set is the first to be returned.
In Map, the put method updates or creates a key-value pair.
The Map interface does not require the creation of a class before creating a collection, and the search can be done directly by key, eliminating the need for for loops.
Examples of older implementations include Vector (synchronized) and HashTable (synchronized and not allowing nulls).
The above is the detailed content of Collection in Java. For more information, please follow other related articles on the PHP Chinese website!