collections or containers usually we use arrays to hold some basic data types, arrays are types supported by compilers, but one obvious disadvantage of arrays is that they have fixed sizes, and in general, only when the program is running can we know the exact number to save. The Java class Library provides a fairly complete set of container frameworks (collections framework) to address this problem. The basic types are list, Set, queue, and map. These object types are also known as collection classes, but because Java uses the name collection to refer to a subset of the class libraries, it is generally used to address them in a wider range of terms "containers". The basic task of a container is to save the object, or more accurately, to save the object's reference. However, the implementation of the various containers is very different, no one container is absolutely excellent to adapt to all the application scenarios, so we need to understand the various containers of the underlying implementation, and then according to the actual situation to choose the appropriate container. The large collections framework can be seen from the classification of the container below, and this does not include the implementation of the queue interface (Priorityqueue and various styles of blockingqueue, etc.), Concurrentmap interface implementations, Copyonwritearraylist and Copyonwritearrayset, Enumset, and Enummap, as well as multiple tool methods in the Collections tool class.
Interface Interface Specification: The collections framework defines some interface specifications, and those commonly used containers inherit certain interface specifications, each of which corresponds to the corresponding abstract class, and if the container provided by the class library does not perform the task well, you can expand the subclass of these abstract classes to customize your own container.
Collection Interface: The collection interface itself inherits the iterator interface by simply representing a set of objects, without specifying the order in which the objects are stored, and whether they contain duplicate elements. Many of the containers below are implemented with collection interfaces, including list, Set, Queue, deque, and so on. It is important to note that the map with the container does not implement the collection interface. The collection interface defines some of the most basic features of a container, including the following interface method definitions: Size method: Returns the number of elements in the container IsEmpty method: Returns whether the container is empty contains method: Returns whether the container contains an element iterator method: Returns a container iterator Add method: Used to add elements to the container Remove method: Used to delete elements in the container clear method: Empty the elements in the container ToArray method: Converts the container class to the appropriate array of objects
List Interface: The list inherits from the collection interface, which represents an ordered container in which elements are stored in an orderly order, allowing duplicate elements to be accessed based on the location of the element. The list interface is based on the collection interface, which is defined by the following methods. Get method: Gets the element at the specified position set: Sets the specified position to the specified element indexof: Gets the position of the specified element in the container listiterator: Returns an iterator of type Listiterator sublist: Gets the container's child container based on the interval position
Common implementation classes for list interfaces are ArrayList, linkedlist, vectors, etc.
set Interface: Inherited from the collection interface, the feature is not to store the same elements, there is no requirement for order, the method definition in the set interface is consistent with the collection interface, and no new method definition is added.
Common set interfaces are implemented with HashSet, SortedSet interfaces
Queue Interface: Inherited from the collection interface, is a container that is defined to hold a series of elements waiting to be carried out, in addition to some basic container operations, the queue queues provide some additional operations for inserting, extracting, and checking.
The following method is defined in the queue interface: Add Method: Inserts the specified element into this queue (if it is feasible immediately and does not violate the capacity limit), returns true on success, and throws IllegalStateException if no space is currently available. Offer method: inserting the specified element into this queue (if it is feasible immediately and without violating capacity limits), when using a capacity-constrained queue, this method is usually better than add (E), which may not be able to insert elements, but only throws an exception. Peek method: Gets the header of this queue without removing it, or null if this queue is empty. Pool method: Gets and removes the header of this queue, or null if this queue is empty. Remove method: Gets and removes the header of this queue. Including Deque, Blockingqueue interfaces are directly inherited with the queue interface
Map Interface: is a collection of mappings for key (key) and value (values), each of which corresponds to a value.
Some basic methods defined in the map interface: Size method: Returns the number of elements in a container put method: Add an element to the container Remove method: Delete the element in the container IsEmpty method: Determine whether the container is empty contains method: Determine whether the container contains the specified element clear method: Clears the element in the container keyset method: Returns the KE of the map Y set, which is a set type that implements some of the classes of the map interface: HashMap, Hashtable, Weakhashmap, Synchronizedmap, CONCURRENTMAP interfaces, etc.
Deque Interface: Inherits from the queue interface, is added in JDK1.6, also called the double-ended queue, that is, you can insert and extract operations at both ends of the queue. Deque interface on the basis of the queue interface, plus the AddFirst, AddLast, Offerfirst, Offerlast and other methods, respectively, that these operations can be done at both ends.
Common Deque Implementation classes: Arraydeque, Concurrentlinkeddeque, LinkedList, Blockingdeque, etc.
Some other interfaces are also included in the collections framework, but are largely extended by the above interfaces, such as SortedSet, SortedMap, Blockingqueue, Blockingdeque, Concurrentmap, etc.
_______________________________________________________________________________________________________________ _______________________________________________________
Some of the common implementation classesThe class library provides some very useful container implementation classes based on the above interfaces and is well qualified for most of the scenarios in daily development.
HashSet: The implementation of the hash table based on the set interface is a comprehensive implementation of the set interface. The order of the elements in the container is unordered, the underlying is based on the HASHMAP implementation, the key represents the value of the set container China, and value is the same object, HashMap the Put method.
TreeSet: Based on a red-black tree, inherited from the Navigableset interface, Navigableset is the extension of sortedset, with a navigation method that reports the closest match to a given search target.
Linkedhashset: Inherits and HashSet class, implements the set interface, has the predictable iteration orderSet
interface to the hash table and the link list implementation. This implementation andHashSet
, the latter maintains a double-link list that runs on all items. This list of links defines the order of iterations, that is, the order in which the elements are inserted into the set (insert order).
ArrayList: The list interface is implemented, the underlying is based on an array, random access to the element is faster, but the insertion and deletion operations are slow.
LinkedList: Implementation of the list interface and Deque interface, the bottom is a double linked list implementation, for the insertion and deletion of elements faster, but the performance of random access is poor. When used through a queue, LinkedList behaves as a FIFO queue.
Arraydeque: Implements the Deque, is a very efficient array-based implementation of the Deque interface.
Priorityqueue: is an unbounded priority queue based on the priority heap
HashMap: The implementation of the map interface based on the hash table, allowing null values and keys to be used, in addition to non-synchronous and nullable, and Hashtable approximately the same.
TreeMap: Based on the navigablemap implementation of the red-black tree, where the mappings are sorted according to the natural order of their keys or the comparator provided when they are created.
Linkedhashmap: A hash table and a linked list implementation of the map interface, with an expected iteration order.
_______________________________________________________________________________________________________________ __________________
Packaging Implementation ClassA series of static internal class containers are provided in the Java.util.Collections tool class, which also implements the collection interface and has its own characteristics, which can be divided into the following three categories: Collections.unmodifiableInterface: Returns a container of the specified interface type, but does not allow the user to modify them. Equivalent to an unmodified view of the original container. Collections.synchronizedInterface: Returns a container of the specified interface type, but is thread-safe. Collections.checkedInterface: Returns a dynamic type security view for the specified interface. Attempting to insert an element of the wrong type will cause the classcastexception to be thrown immediately. _______________________________________________________________________________________________________________ ____________________
the realization based on the characteristic purpose:
Weakhashmap: We say that containers are used to hold objects, more accurately, to hold references to objects, and that if a container of a long life cycle keeps a reference to a useless object, the GC cannot be recycled. Weakhashmap toWeak keysImplementation of a hash table-basedMap。 InWeakhashmap, when a key is no longer in use, its entry is automatically removed. More precisely, for a given key, the presence of its mapping does not prevent the garbage collector from discarding the key, which makes the key a terminating, terminated, and then recycled. When a key is discarded, its entry is effectively removed from the map, so the behavior of the class is related to the otherMapimplementation is different.Identityhashmap: This class uses a hash table to implementMapinterface, which uses referential equality instead of object equality when comparing keys (and values). In other words,IdentityhashmapWhen and only if the(K1==K2)When the two keys are consideredK1AndK2Equal.copyonwritearraylist: A thread-safe variant of ArrayList, where all mutable operations (Add、SetAnd so on) are implemented by a new copy of the underlying array, which typically requires a lot of overhead.CopyonwritearrayseT: Internal use of copyonwritearraylist setEnumset: A dedicated set implementation that is used with enumeration types. All keys in an enumeration set must come from a single enumeration type, which is explicitly or implicitly specified when the set is created.
Enummap: A dedicated map implementation that is used with enumeration types. All keys in an enumeration map must come from a single enumeration type, which is explicitly or implicitly specified when the mapping is created. An enumeration map internally represents an array. This representation is very compact and efficient.
——————————————————————————————————————————————————————————————————————implementation classes for concurrent use:
Concurrentlinkedqueue: An unbounded thread-safe queue based on a linked node that sorts elements according to the FIFO principle.Concurrenthashmap: Supports getting full concurrency and updates of expected adjustable concurrent hash tables, which are based on non-blocking implementations.Linkedblockingqueue: A blocking queue that is based on a linked node, and also sorts elements by FIFO.Priorityblockingqueue: An implementation of a blocking queue with a priority.
Other concurrent containers can be found under the Java.util.concurrent package
Java Collections Framework