Q: What is the J**a Collections framework?
A: The j**a collections framework contains a large number of collection interfaces, as well as the implementation classes of these interfaces and the algorithms that manipulate them (e.g., sorting, finding, inverting, replacing, copying, taking the smallest element, taking the largest element, etc.).Specifically, data structures such as list, queue, set, stack, and map are provided to store key-value pairs。The list, queue, set, and stack are all inherited from the collection API.
Collection is the foundation of the entire collection framework, which stores a set of objects, represents different types of collections, and its role is only to provide a basic interface for maintaining a set of objects.
The following describes the three interfaces: set, list, and map.
set stands for the concept of a set in a mathematical sense。Its main features are:Elements in a collection cannot be duplicated, so each element deposited in the set must define the equals() method to ensure the uniqueness of the object. The interface has two implementation classes: hashset and treeset。where the treeset implements the sortedset interface, so the elements in the treeset container are ordered.
List is also known as an ordered collection. It holds the objects in the order in which they enteredSo it can be applied to each of the listsPrecise control over the insertion and deletion of elements。At the same time, it canSave duplicate objects。LinkedList, ArrayList, and Vector all implement the List interface.
A map provides a data structure that maps from a key to a value. It is used to hold key-value pairs, where the values can be repeated, but the keys are unique and cannot be repeated。There are several classes that implement this interface in the J**A class library: HashMap, TreeMap, LinkedHashMap, WeakHashMap, and IdentityHashMap. Although they all implement the same interface, the execution efficiency is not exactly the same. Specifically, the hashmap is implemented based on a hash table, and the hashcode of the object can be used for fast query. LinkedHashMap employs lists to maintain internal order. Treemap is implemented based on the data structure of the red and black tree, and the internal elements are arranged as needed.
The frame class diagram of the collection is shown in the following figure: