The collection framework is mainly divided into collection classes and mapping classes: The collection class stores a set of pure data, such as a set of objects. The mapping class stores a mapping of key-value pairs, but you can get the key collection view, Value collection view, and key-value pair collection view of key-value pairs in the appropriate way. For these two classes, the top-level interfaces of the collection framework are collection and map interfaces, respectively.
the inheritance hierarchy for the collection class is:
the inheritance hierarchy for a mapping class is:
The above solid lines represent inheritance (extends), and dashed lines represent implementations (implement). The inheritance hierarchy of the collection class and the mapping class is added together, and the complete set frame is constructed.
Collection Framework Design concept : to provide a set of "small and beautiful" API. The API needs to be programmer-friendly and add new functionality to enable programmers to get started quickly.
To achieve this design concept, the following points need to be achieved:
first, to ensure that the top-most core interface is small enough : the topmost interface (that is, collection and map interface) only need to implement a set should have the basic operation. The topmost interface does not differentiate whether the collection is mutable (mutability), whether it can be changed (modifiability), and whether the small differences in size (resizability) can be changed. Some operations are optional and can be thrown when implemented to UnsupportedOperationException
indicate that the collection does not support the operation. The implementation of the collection must declare in the document that those operations are not supported.
The methods of the core interface collection interface are as follows:
The method of the core interface map interface is as follows:
Note: There is an inner class in the map interface, the interface entry, which represents the key-value pair object.
To expand the core interface as needed : in practical applications there may be special operational requirements for specific collections, so only the extends core interface can be extended.
Core interface Collection The main sub-interfaces are:
1. List interface: The data elements in the collection interface of the core interface are stored out of order. The list interface directly inherits the collection interface, increasing the manipulation of the position of the element, allowing for duplicate elements. The collection class that implements the interface is a collection of data that is stored as a list, and allows precise control over the position of each element of the insert list. So the interface adds a lot of methods that contain the parameter index.
2, set interface: directly inherit from the collection interface, the extension of the place is required elements can not be duplicated. If an already existing element is inserted into the collection class that implements the set interface, the insertion fails. If you further require that the stored elements be sorted, you can continue to extend the set interface to get the SortedSet interface that inherits from the set interface.
3, queue interface: directly inherit from the collection interface. It is not the same as other interfaces: The queue is primarily used to store data, rather than processing data (A collection designed for holding elements prior to processing). is a collection used to hold an element before it is processed. The queue provides a second form of INSERT, FETCH, and check operations in the collection interface: one throws an exception (when the operation fails), and the other Returns a special value (null or false, depending on the action). For example, the insert operation in the collection interface is add (e e), and queue provides an additional insert method offer (e e). Queues typically (but not necessarily) order individual elements in FIFO (first-in, out-of-order), except for priority and LIFO queues (or stacks). This also requires that the appropriate method be extended.
The main sub-interfaces of the core interface map are:
1, SortedMap interface: If I want not only to store key-value pairs, but also want to set the key value pairs by key to sort, then you can directly extend the map interface, get the SortedMap interface.
Third, the implementation of the backbone of the interface method : With the above interface level, if I want to design a set, directly implement these interfaces. However, the amount of code to rewrite all the abstract methods is still relatively large, the JDK in order to facilitate the design of the collection, has been implemented in advance the main operation of these interfaces, that is, the corresponding abstract class. For example, the main method of implementing the collection interface Abstractcollection abstract class, implementation of the Main method of map interface Abstractmap Abstract class, the implementation of the list interface Abstractlist abstract class and so on. If you want to design a custom collection class, you can inherit these abstract classes directly.
Iv. provide a concrete implementation class for the set framework :
The specific implementation classes of the collection class system include:
The specific implementation of the list interface:
1, ArrayList class: Inherits from the Abstractlist class, realizes is the list interface. The length can be dynamically changed (in-place expansion) of the array linear table class, because it is based on an array implementation, so the advantage is: Random access speed extremely fast (O (1)), but the disadvantage is: not suitable for frequent insert and delete operations (O (n)). Features: initialized with the default constructor, the ArrayList length is 10, and the capacity is changed to "raw capacity *3/2+1" when expanding in-place. ArrayList is not thread-safe.
2, Vector class: Inherit from the Abstractlist class, the implementation is the list interface. and ArrayList basically the same, the difference is: ① expansion plan is changed to the original capacity of twice times. ② is thread-safe. Vectors are generally used instead of ArrayList in multi-threading.
3, LinkedList class: Inherits from the Abstractsequentiallist class, realizes is the list and the queue interface. is a collection class implemented in the form of a list. Because it is based on a two-way circular list implementation, the advantage is that the insert and delete operations are efficient (O (1)) and the disadvantage is that random access is slow because the linked list (O (n)) needs to be traversed.
The specific implementation of the set interface:
1, HashSet class: Inherits from the Abstractset class, realizes is the set interface. Supported by a hash table, a class that stores no duplicate data based on a hash algorithm. In fact, HashSet Bottom is based on hashmap implementation. HashMap Store is a key value pair, the key can not be repeated, if you see the key alone, the value is set to a fixed value, in fact, HashMap is set set of keys! The iteration order of the collection is not guaranteed, especially if the iteration order of the collection is not guaranteed to be permanent (because the hash resize can occur).
2, Linkedhashset class: Inherits from the HashSet class, realizes is the set interface. By looking at the source code of Linkedhashset, it can be found that the bottom layer is based on linkedhashmap. for Linkedhashset, the main difference between it and hashset is that the elements stored in Linkedhashset are added to the structure of the chained table on the basis of the hashing algorithm.
3, TreeSet class: Inherits from the Abstractset class, realizes is the SortedSet interface. TreeSet is a sort of binary tree. Values in the set set are sorted by the size of the value. The underlying algorithm is based on red-black trees. The main difference between TreeSet and HashSet is that the elements in TreeSet are sorted by the relevant values and are supported by the hash table.
The contents of the hash table refer to the previous blog: http://www.cnblogs.com/shakinghead/p/7717705.html
the specific implementation of the queue interface: 1. Priorityqueue class: Represents an unbounded priority queue based on the priority heap. The priority queue is unbounded, but has an internal capacity that controls the size of the array used to store the queue elements. It is usually at least equal to the size of the queue. As you continue to add elements to the priority queue, their capacity increases automatically.
The specific implementation classes of the mapping class system include:
1, HashMap class: Inherit the Abstractmap class, realize the map interface. HashMap is based on the hash table, if the key hash value is the same, the use of the chain list method to save (zipper method). When you create a new HashMap, the default is to initialize an empty hashmap with a size of 16 and a load factor of 0.75. HashMap allows at most one key to be null, allowing multiple value to be null.
2, Linkedhashmap class: Inherit from HashMap, realize the map interface. It is also based on the hash algorithm, but the difference between Linkedhashmap and HashMap is that Linkedhashmap is not a "list of linked lists" but a doubly linked list. This type of thread is not secure, and if you want to use it in multiple threads, you need to use the Collections.synchronizedmap method for external synchronization.
3, Hashtable class: Inherits from the Dictionary class, realizes the map interface. This class implements a hash table that maps the keys to the corresponding values. Hashtable does not allow any null elements, it is thread-safe.
4, TreeMap class: Inherit the Abstractmap class, realize the SortedMap interface. The sorting of key-value pairs is implemented based on the red-black tree. This type of thread is unsafe, and if used in multi-threading, you should "wrap" the map using the collections.synchronizedmap method.
add : 1. The collection interface has a method ToArray () that transforms the collection class into an array, so any collection class can implement the interaction of the collection and the array through this method.
2, Map interface has a method to get the view of the key collection keyset (), there is a way to get the value collection view of values (), there is a way to get the value of the collection View EntrySet (), through the three methods to implement the mapping class and collection class interaction.
Summary : The collection framework is a series of interfaces, interfaces of the backbone of the implementation of abstract classes and concrete implementation of the template. The topmost interface contains the core method, which derives the sub-interfaces and sub-interfaces of the subinterfaces according to the actual requirements. In order to facilitate the implementation of the interface, the initial implementation of these interfaces, the backbone of the implementation of the abstract class. Finally, the complete implementation classes are packaged to serve different storage and operational requirements.
The set frame is divided into two branches, a collection class system and a mapping class system, which can be interacted with (transformed). Collection classes can also interact with arrays (conversions).
The above is my personal collection Framework learning summary, over.
Java Collection Framework Overview