Collections Framework Series Overview

table of Contents

1 Overview

2 inheritance system

 

1 Overview

In general, Java container can be divided into four parts:

  • List collection
  • Set collection
  • Queue collection
  • Map collection

In addition to the above four kinds of set, there is a special tools:

  • Tools (Iterator iterator, Enumeration enumeration class, Arrays and Collections)

FIG collection overview is given below to perform the intuitive feel

 2 inheritance system

Before you start talking specific collection system, I would like to explain the basic class structure Collection framework. Because both the collection of List, Set collection or Map to this collection are based.

  • First, the top level is the Collection interface.

Collection can be seen that the interface defines the most basic set of operations, such as: set size determination, it is determined whether the collection is waited in vain. List, Set, Queue inherit this interface.

  • Then, AbstractCollection also inherited the Collection interface.

As can be seen from this class name, which is an abstract class. AbstractCollection of the Collection interface some common ways of doing the implementation. For example: determining whether the method is empty, the method comprising determining whether an element or the like.

By inheriting AbstractCollection interface, you can write less a lot of unnecessary code, which is the code abstract design most commonly thought. AbstractCollection is the most basic category, all other set of things have inherited this abstract class.

2.1 List collection

List stored set is an ordered set of data, the data structure is characterized by: fast read, modify slower, adapted to read much less write scene changes. List collection class inheritance structure is as follows:

We can see that in addition to the Collection and AbstractCollection, we have AbstractList List interfaces and abstract classes. List interface which is the uppermost layer of abstraction List collection, which inherited the Collection interface, in fact, represents a collection. The AbstractList is a collection of abstract implement List, implements many common operations.

List implement the entire collection can be divided into red, yellow, green and three blocks. The red part is a list of List collection implementation, the green part is combined list List to achieve, while the yellow part is thread-safe version of the List to achieve the set list.

2.1.1 list implementation

ArrayList class List is commonly implemented, the underlying array is implemented. Element which reads the time complexity is O (1), the write element modification time complexity is O (N). We will explain in detail in the following sections, do not do in-depth here.

2.1.2 List Security Implementation

Vector class List are very commonly used to achieve, and ArrayList data structure is very similar. But the biggest difference is that with an ArrayList: Vector is thread-safe, but ArrayList is not thread safe.

Stack class is based on the Vector, but also to achieve a two-way queue. So in addition to its thread-safe outside, which is an advanced after the implementation of List.

Finally, we summarize, List collection of the most critical implementation class are a few:

  • ArrayList: List collection of classic achieve.
  • Vector: a list of the Classical collection, thread-safe, and the corresponding ArrayList.
  • Stack: Stack classic structure to achieve, last-out data structure. Inherited Vector, thread-safe.
  • LinkedList: classic linked list structure to achieve.

2.1.3 list implementation

LinkedList is a classic linked list implementation. LinkedList inherits AbstractSequentialList abstract class. AbstractSequentialList abstract class is abstract literally continuous list. The focus here is
sequential word, showing the data structure is continuous (list). Comments can also be seen from the source meant.

A skeletal Implementation class the Provides the this of at The  List  interface to the Minimize at The effort required to Implement the this interface backed by A "Sequential Access" the Data Store (SUCH AS A linked List).
(Paraphrasing) if you want to implement a continuous storage (list) the List, then the abstract class lets you reduce a lot of work enough.

In fact, it can be seen from the name, AbstractSequentialList in fact, is an abstract achieve continuous list (list) of. AbstractSequentialList abstract class to do a lot of work, so that the subsequent list implementation easier. AbstractSequentialList can see from the comments, if you want to implement a linked list, you only need to implement the method and size listIterator method on it.

2.2 Set collection

Set elements are stored in the collection do not overlap, but the stored sequence is disordered. The following is a configuration diagram of Set class inheritance set:

Similar to the List collection, is a Set interface extends the Collection interface. A AbstractSet abstract class implements the Set interface, inherited AbstractCollection abstract class. This part is completely the same and List.

Set to achieve the set can be divided into two blocks, one is ordered to achieve (the red part) Set the set, the other is a hash achieve (yellow part) Set collection.

2.2.1 orderly realization (TreeSet)

  • SortedSet interface extends the interface Set, TreeSet implements SortedSet.

We know Set elements of the collection is disordered, and the interface is defined SortedSet interface Set an ordered collection. The TreeSet is the realization of a SortedSet.

2.2.2 Hash achieve (HashSet, LinkedHashSet)

HashSet is a classic hash implements the Set interface. But Set elements of the collection is unordered, in order to maintain insertion order Set collection, people created a LinkedHashSet. LinkedHashSet HashSet on the basis of the list is maintained insertion order.

Here we summarize all realize Set collection:

  • TreeSet: Ordered Set collection implementation.
  • HashSet: Hash Set collection implementation.
  • LinkedHashSet: Hash Set collection implement, maintain the element insertion order.

2.3 Queue collection

Queue is a special linear form, characterized by a FIFO data structure. Queue-based architecture as shown below:

First, Queue interface extends the Collection interface. Queue interface has a basic set of operations on the basis of the defined basic operations of this queue data structure. We can see the offer, poll and other methods are unique to queue operations.

Next, AbstractQueue abstract interface to achieve Queue. This data structure for the queue, add, delete elements of action are not the same. In AbstractQueue abstract class will implement the basic operation of the queue again. For example, add a method AbstractQueue on the method and add in AbstractList has different implementations.

Shown above, the overall structure Queue class can be divided into two parts yellow, red. Red is part of an orderly realization Queue interfaces, there PriorityQueue the implementation class. Yellow part is to achieve the Deque (double-ended queue), there are two ArrayDeque LinkedList and implementation class.

2.3.1 orderly realization

AbstractQueue PriorityQueue is a concrete implementation of the abstract class.

PriorityQueue priority queue indicates that re-sorted by size queue element. When the call peek () or the pool () method when the head is removed from a queue, is not taken into the first element of the queue, but remove the smallest element of the queue.

2.3.2 achieve two-way

  • First, we will see Deque interface. Deque (double ended queue) is a bidirectional queue means, the element can be operated at the head or tail.
  • Finally, we see a concrete realization of LinkedList and ArrayDeque are Deque interface.

LinkedList we said before, is not a chain, but it is a two-way queue. Therefore LinkedList has the dual characteristics of List and Queue. ArrayDeque circular queue is a two-way, which is implemented by the underlying array. More, we will explain in the queue section.

Finally, we summarize several common implementation class Queue system:

  • PriorityQueue: priority queue
  • LinkedList: two-way queue implementation
  • ArrayDeque: two-way circular queue implementation

2.4 Map collection

Map collection of the List, Set, Queue are quite different, in fact, similar to the data structure of key / value is.

  • First, Map interface is the top-level interface. And List, Set, Queue Similarly, Map interface definition is the operation of the hash table data structure. For example, we commonly put, get, keySet and so on.
  • Next, there AbstractMap abstract class. And List and the like, AbstractMap abstract implement the Map interface. As shown above, the structure of the entire class Map can be divided into a set of red, yellow, green three.

2.4.1 Hash achieve

Red can be seen as part of a hash Map implementation.

  • AbstractMap have a specific category HashMap. HashMap is a realization AbstractMap hash-based algorithms.
  • Then, LinkedHashMap and WeakedHashMap inherited HashMap.

LinkedHashMap HashMap is further achieved, which is stored as a linked list insertion sequence elements in the HashMap. WeakedHashMap the HashMap is further achieved, and the difference is HashMap: weak references cited in WeakedHashMap, useless if too long, will be automatically recovered.

2.4.2 orderly realization

Yellow can be seen as part of an orderly realization Map collection.

  • First, SortedMap interfaces inherited Map interface. As with the Set, Map elements are not in order, SortedMap is orderly Map interface definition.
  • Then, NavigableMap inherited SortedMap interface. NavigableMap interface defines the logic to find, easy to follow to achieve.
  • Finally, TreeMap is the realization NavigableMap interface.

In fact, TreeMap is a red-black tree-based Map implementation.

See here, Map reading the entire class structure half. The other half is based on the Dictionary-based implementation (the green part). But in fact achieve Map Dictionary is old, now abandoned. We can see relevant tips from comments in the source code.

NOTE: This class is obsolete (obsolete) New implementations should implement the Map interface , rather than extending this class..
This class has been abandoned, new implementations should implement the Map interface, rather than extending this class.

So for the realization in Dictionary, and we do not intend to in-depth explanation.

Here we sum up all the implementation class Map collection:

  • HashMap: Map collection of classic hash implementation.
  • LinkedHashMap: On the basis of the HashMap, adds to insert elements of list maintenance.
  • WeakedHashMap: HashMap on the basis of the strong references become weak.
  • TreeMap: Ordered achieve Map collection.

2.5 Tools

There are a set of tools: Iterator iterator, ListIterator iterator, Enumeration enumeration class, Arrays and Collections classes.

2.5.1 Iterator iterator

An Iterator is used to traverse a sequence and select objects. Java's Iterator only one-way move. Can be seen in ArrayList, WeakHashMap other collection classes implement this interface to set different types of traversal.

2.5.2 ListIterator iterator

ListIterator inherited the Iterator interface, so it has more powerful features that it enables two-way movement. However, its name can be seen, it can only apply to traverse the List collection.

2.5.3 Enumeration sheet 举类

It was introduced in JDK 1.0 interface. And Iterator role, just as through the collection. But Enumeration Iterator function than less. Enumeration can only be used in the Hashtable, Vector, Stack in. This interface has been substituted with a conventional iterator, although Enumeration has not been abandoned, but the code has been rarely used.

The official document also recommended Iterator interface instead of Enumeration interface.

2.5.4 Arrays

Java.util.Arrays class can easily operate the array, it provides all the methods are static.

2.5.3 Collections

java.util.Collections containing various tools is a set of operations related polymorphic static method, the serving Java Collection framework.

3 summary

We spent a lot of space to explain the collection of List, Set collection, Map collections, Queue collections and Iterator and other tools. We conducted a detailed analysis of this set of class structure, so that a better understanding of the relationship between them.

Sometimes we want to learn so much dim it. I have a friend only a common ArrayList, HashMap can ah. For this question, I would like to share a few harvest.

First, make you more familiar with the differences between classes.  If we only use one or two classes, then we do not know when and what type. For example: when to use HashMap, when to use Hashtable? Iterator interface What is the role? JDK source named What are the characteristics?

Second, convenient source for expansion.  When we delve into the set of things, we know that the original is the List List interface of this data type definitions, and AbstractList List of abstraction is achieved. So if we want to achieve a custom List structure, then we can inherit directly AbstractList class, so as to achieve rapid implementation. But if you do not have in-depth study of it? You may only be written from scratch, so much energy was wasted ah. You learn in this way, then for you to expand Spring is also a good source of help.

In the next article, we'll dive into the specific implementation of each set.

 

Guess you like

Origin www.cnblogs.com/youngao/p/12518875.html