Inheritance structure of java container classes

summary:  Inheritance structure of java container class

The Java Container Class Library defines two containers with different concepts, Collection and Map

(The Jdk source version in the text is jdk1.8.0_101 without special instructions)

public interface Collection<E> extends Iterable<E> {

As you can see, java defines the Collection interface and the basic operation methods of the internal collection. Collection can perform operations such as adding elements to the end of the collection and deleting specified elements by default. The List, Set, and Queue interfaces all inherit from Collection and define their own different methods. If you want to learn Java, you can come to this group. The first is 220, the middle is 142, and the last is 906. There are a lot of learning materials for download.

public interface Map<K,V> {

Map internal interface Entry<K,V> corresponds to the key-value pair of Map.

Specific introduction

iterator

Let's first introduce iterators. The iterator itself is also a design pattern. The original intention of the design is that there are many kinds of container implementations. If we want to traverse the container, we should not care about the details of the container implementation first, and the traversal operation should be lightweight. Iterators unify access to a container while creating it at a low cost. It's worth noting that iterators can only move in one direction.

public interface Iterator<E> {

Get the iterator of the container through the iterator() method of the container

The iterator's next() gets the next element

hasNext() determines whether there are more elements

remove() removes the specified element

ListIterator

ListIterator is an extension of Iterator, which is used for accessing various List classes and supports bidirectional movement.

Collection

List

List promises to maintain elements in a specific sequence. The List interface adds a large number of methods to Collection, making it possible to insert and remove elements in the middle of List.

public interface List<E> extends Collection<E> {

There are two types of List, ArrayList and LinkedList

List type advantages and disadvantages underlying implementation
ArrayList Random access to elements is faster Slow insertion and deletion of intermediate elements array
LinkedList Insertion and deletion of intermediate elements, optimization of sequential access Random access to elements is slower Doubly linked list

Set

Set does not save duplicate elements and is often used to find elements quickly. It is worth mentioning that Set has exactly the same interface as Collection without any additional functionality. The stored elements must define the equals() method

The Set type uses the underlying implementation of the scene
HashSet For quick lookup, elements must define hashCode() linked list
TreeSet To maintain order, elements must implement the Comparable interface red-black tree structure
LinkedHashSet HashSet maintaining order, elements must define hashCode() linked list

Queue

In addition to concurrent applications, the only two implementations of Queue are LinkedList and PriorityQueue, where LinkedList implements the List and Deque interfaces at the same time. They differ in sorting behavior rather than performance.

​​​​​​​

Original link

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326567479&siteId=291194637