Summary: Inheritance structure for Java container classes
The Java Container Class library defines two different concepts of containers, collection and map
(The JDK source code version of the article has no special instructions are jdk1.8.0_101)
Public interface Collection<e> extends iterable<e> {
As you can see, Java defines the basic operating methods of the collection interface and the internal collection, and collection by default can add elements to the end of the collection, delete the specified elements, and so on. The List, Set, and queue interfaces inherit from the collection and define their own different methods. If you want to learn Java can come to this group, the first is 220, the middle is 142, the last is 906, there is a large number of learning materials can be downloaded.
Public interface Map<k,v> {
The map internal interface entry<k,v> corresponds to the key value pairs of the map.
Specific introduction
Iterators
Let's introduce an iterator. The iterator itself is also a design pattern, the original idea is: the implementation of the container by many kinds, and we want to traverse the container, first of all should not care about the details of the container implementation, and then the traversal operation should be lightweight. Iterators unify the way the container is accessed, and the cost of creating it is minimal. It is important to note that iterator can only be moved one way.
Public interface Iterator<e> {
The container's iterator is obtained through the container's iterator () method
Next () of the iterator gets the next element
Hasnext () Determine if there are elements
Remove () deletes the specified element
Listiterator
Listiterator is an extension of iterator for various list class accesses and supports bidirectional movement.
Collection
List
The List promises to maintain elements in a particular sequence. The list interface adds a number of methods based on the collection, allowing you to insert and remove elements in the middle of a list.
Public interface List<e> extends collection<e> {
There are two types of list,arraylist and LinkedList
| List Type |
Advantages |
Disadvantages | The
underlying implementation |
| ArrayList |
Faster random access elements |
Slow insertion and deletion of intermediate elements |
Array |
| LinkedList |
Insertion and deletion of intermediate elements, optimization of sequential access |
Slow random access elements |
Doubly linked list |
Set
Set does not save duplicate elements, which are often used to quickly find elements. It is worth mentioning that the set has exactly the same interface as the collection, without any additional functionality. The deposited element must define the Equals () method
| set Type |
Usage Scenarios | The
underlying implementation |
| HashSet |
Quick Find, element must define HASHCODE () |
Linked list |
| TreeSet |
Keep order, element must implement comparable interface |
Red-black tree structure |
| Linkedhashset |
HashSet of maintenance order, elements must be defined hashcode () |
Linked list |
Queue
In addition to concurrent applications, the only two implementations of the queue are LinkedList and Priorityqueue, where LinkedList simultaneously implements the list, deque interface. They differ in the sort behavior rather than the performance.
???????
Original link
Inheritance structure of Java container classes