Introduction to Java collection class in Chinese

  • 2020-04-01 02:30:36
  • OfStack

Java collections are toolkits provided by Java that contain commonly used data structures: collections, linked lists, queues, stacks, arrays, maps, and so on. The Java collections toolkit location is java.util.*
Java Collections can be divided into four main parts: List, Set, Map, utility classes (Iterator Iterator, Enumeration class, Arrays, and Collections), and.
Java collections toolkit diagram (below) :
General description:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/20131105093834.jpg? 201310594757 ">
Take a look at the frame diagram above and grab its trunk, Collection and Map.
1 Collection is an interface, a highly abstract Collection that contains the basic operations and properties of a Collection.
  A Collection contains two branches, List and Set.
  (01) List is an ordered queue, each element has its index. The index value of the first element is 0.
                  List implementation classes are LinkedList, ArrayList, Vector, Stack.
  (02) Set is a Set that does not allow duplicate elements.
                  The Set implementation classes are HastSet and TreeSet. A HashSet depends on a HashMap, which is actually implemented through a HashMap; TreeSet depends on TreeMap, which is actually implemented through TreeMap.
2 Map is a mapping interface, that is, key-value key-value pair. Each element in the Map contains "a key" and "the value corresponding to the key."
    AbstractMap is an abstract class that implements most of the apis in the Map interface. HashMap, TreeMap, WeakHashMap are all subclassed from AbstractMap.
    While Hashtable is inherited from the Dictionary interface, it implements the Map interface.
Next, look at Iterator. It is a tool for traversing collections, that is, we typically traverse collections through the Iterator Iterator. We say that Collection is dependent on Iterator because the implementation class of Collection implements the Iterator () function, which returns an Iterator object.
ListIterator exists specifically for traversing a List.
Consider Enumeration, an abstract class introduced in JDK 1.0. Like Iterator, it iterates over a collection; But Enumeration has less functionality than Iterator. In the block diagram above, Enumeration can only be used in Hashtable, Vector, Stack.
Finally, look at Arrays and Collections. They are two utility classes that manipulate arrays and collections.
With the overall framework above in place, let's analyze each class individually.

Introduction of the Collection
Let's take a look at the diagram of some of the framework classes for Collection:
< img SRC = "border = 0 / / files.jb51.net/file_images/article/201311/20131105094054.jpg? 201310594827 ">
Collection is an interface with two main branches: List and Set.
List and Set are both interfaces and they inherit from the Collection. A List is an ordered queue, in which there can be repeated elements. And Set is a Set of mathematical concepts, there are no repeating elements in Set!
Both List and Set have their own implementation classes.
  For convenience, we've abstracted the AbstractCollection class, which implements most of the functions in the Collection; This way, in the implementation class of Collection, we can omit duplicate coding by subclassing AbstractCollection. Both AbstractList and AbstractSet are subclassed from AbstractCollection, the concrete List implementation class is subclassed from AbstractList, and the Set implementation class is subclassed from AbstractSet.
  In addition, there is an iterator() function in the Collection that returns an iterator interface. Typically, we iterate over collections through the Iterator Iterator. ListIterator is unique to the List interface, where a ListIterator object is returned through ListIterator().
  Next, let's look at the various interfaces and abstract classes; Then, we'll look at the implementation classes in more detail.
The definition of Collection is as follows:


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

  It is an interface, a highly abstract collection that contains the basic operations of a collection: add, delete, empty, iterate (read), whether it is empty, get the size, protect an element, and so on.

All subclasses of the Collection interface (direct and indirect) must implement two types of constructors: constructors with no arguments and constructors with arguments of Collection. A constructor with arguments that can be used to convert the type of a Collection.


//A Collection of API
abstract boolean         add(E object)
abstract boolean         addAll(Collection<? extends E> collection)
abstract void            clear()
abstract boolean         contains(Object object)
abstract boolean         containsAll(Collection<?> collection)
abstract boolean         equals(Object object)
abstract int             hashCode()
abstract boolean         isEmpty()
abstract Iterator<E>     iterator()
abstract boolean         remove(Object object)
abstract boolean         removeAll(Collection<?> collection)
abstract boolean         retainAll(Collection<?> collection)
abstract int             size()
abstract <T> T[]         toArray(T[] array)
abstract Object[]        toArray()


 
2 List profile
The definition of List is as follows:

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

List is an interface inherited from the Collection, that is, List is a kind of Collection. A List is an ordered queue. Each element in a List has an index. The index value of the first element is 0, and the index value of the following elements is +1. Unlike sets, lists allow duplicate elements.
The official description of the List is as follows:
A List is A collection which maintains an ordering for its elements. Every element in the List has an index, Each element can thus be accessed by its index, with the first index being zero. Normally, Lists allow duplicate elements, as compared to Sets, where elements have to be unique.

 
About the API. Since List is inherited from the Collection interface, it naturally contains all the function interfaces in the Collection. Since a List is an ordered queue, it also has an additional API interface. There are "add, delete, get, modify the specified location of the element", "get the List of child queue" and so on.

//A Collection of API
abstract boolean         add(E object)
abstract boolean         addAll(Collection<? extends E> collection)
abstract void            clear()
abstract boolean         contains(Object object)
abstract boolean         containsAll(Collection<?> collection)
abstract boolean         equals(Object object)
abstract int             hashCode()
abstract boolean         isEmpty()
abstract Iterator<E>     iterator()
abstract boolean         remove(Object object)
abstract boolean         removeAll(Collection<?> collection)
abstract boolean         retainAll(Collection<?> collection)
abstract int             size()
abstract <T> T[]         toArray(T[] array)
abstract Object[]        toArray()
//Compared with Collection, List has a new API:
abstract void                add(int location, E object)
abstract boolean             addAll(int location, Collection<? extends E> collection)
abstract E                   get(int location)
abstract int                 indexOf(Object object)
abstract int                 lastIndexOf(Object object)
abstract ListIterator<E>     listIterator(int location)
abstract ListIterator<E>     listIterator()
abstract E                   remove(int location)
abstract E                   set(int location, E object)
abstract List<E>             subList(int start, int end)


 
3 Set profile
The definition of Set is as follows:

public interface Set<E> extends Collection<E> {}

Set is an interface inherited from the Collection, that is, Set is also a kind of Collection. A Set is a Set that has no repeating elements.
About the API. Set has exactly the same API as Collection.

//The Set of apis
abstract boolean         add(E object)
abstract boolean         addAll(Collection<? extends E> collection)
abstract void             clear()
abstract boolean         contains(Object object)
abstract boolean         containsAll(Collection<?> collection)
abstract boolean         equals(Object object)
abstract int             hashCode()
abstract boolean         isEmpty()
abstract Iterator<E>     iterator()
abstract boolean         remove(Object object)
abstract boolean         removeAll(Collection<?> collection)
abstract boolean         retainAll(Collection<?> collection)
abstract int             size()
abstract <T> T[]         toArray(T[] array)
abstract Object[]         toArray()


 
4 AbstractCollection
The definition of AbstractCollection is as follows:
Public abstract class AbstractCollection< E> Implements Collection< E> {}
AbstractCollection is an abstract class that implements functions in a Collection other than iterator() and size().
The main purpose of AbstractCollection: it implements most of the functions in the Collection interface. This makes it easy for other classes to implement collections, such as ArrayList, LinkedList, and so on, which want to implement Collection interfaces, most of which are already implemented through subabstractcollection.

 
5 AbstractList
An AbstractList is defined as follows:
Public abstract class AbstractList< E> Extends AbstractCollection< E> Implements List< E> {}
AbstractList is an abstract class that inherits from AbstractCollection and implements the List interface. It implements the List functions except size() and get(int location).
The main purpose of AbstractList: it implements most of the functions in the List interface. This makes it easy for other classes to inherit lists.
In addition, the AbstractList abstract class implements the iterator() interface, compared to AbstractCollection.

 
6 AbstractSet
AbstractSet is defined as follows:
Public abstract class AbstractSet< E> Extends AbstractCollection< E> Implements Set< E> {}
AbstractSet is an abstract class that inherits from AbstractCollection and implements the Set interface. Since the Set interface is exactly the same as the API in the Collection interface, Set has no separate API of its own. Like AbstractCollection, it implements functions in a List other than iterator() and size().
The main purpose of AbstractSet: it implements most of the functions in the Set interface. This makes it easy for other classes to implement Set interfaces.

 
7 the Iterator
Iterator is defined as follows:

public interface Iterator<E> {}

Iterator is an interface that is an Iterator for a collection. Collections can be iterated over the elements of a collection using an Iterator. Iterator provides an API interface for whether the next element exists, getting the next element, and deleting the current element.
Note: when Iterator iterates through the Collection, it is fail-fast. That is, when A thread A traverses A set through an iterator, if the contents of the set are changed by other threads; Then thread A access to the collection, will throw ConcurrentModificationException, produce fail - fast. We will explain the details of fail-fast later. TODO

//The Iterator API
abstract boolean hasNext()
abstract E next()
abstract void remove()
 


8 a ListIterator
ListIterator is defined as follows:

public interface ListIterator<E> extends Iterator<E> {}

ListIterator is an interface inherited from Iterator, which is a queue Iterator. Dedicated to facilitating lists, it provides forward/backward traversal. Compared to Iterator, it adds API interfaces such as add, whether the previous element exists, get the previous element, and so on.

//ListIterator API
//Interface inherited from Iterator
abstract boolean hasNext()
abstract E next()
abstract void remove()
//New API interface
abstract void add(E object)
abstract boolean hasPrevious()
abstract int nextIndex()
abstract E previous()
abstract int previousIndex()
abstract void set(E object)


Related articles: