Java Collection Class Chinese introduction _java

Source: Internet
Author: User
Tags addall data structures int size object object set set

Java collection is a Java-provided toolkit that contains commonly used data structures: collections, lists, queues, stacks, arrays, mappings, and so on. Java Collection Toolkit location is java.util.*
Java collections can be divided into 4 main parts: List List, set set, map Map, tool class (iterator iterator, enumeration enum class, arrays and collections),.
Java Collection Toolkit framework diagram (as follows):
General Description:

Looking at the frame above, first grab its trunk, collection and map.
The 1 collection is an interface, a highly abstracted set that contains the basic operations and attributes of the collection.
collection contains both the list and set branches.
The list is an ordered queue, and each element has its index. The index value of the first element is 0.
The implementation class of list has LinkedList, ArrayList, Vector, Stack.
Set is a collection that does not allow duplicate elements.
The implementation class for set has Hastset and TreeSet. HashSet relies on HashMap, which is actually implemented through HASHMAP, and TreeSet relies on treemap, which is actually implemented through TREEMAP.
The 2 map is a mapping interface, that is, a Key-value key value pair. Each element in the map contains "one key" and "key corresponding value".
Abstractmap is an abstract class that implements most of the APIs in the map interface. And Hashmap,treemap,weakhashmap are inherited from Abstractmap.
Although Hashtable inherits from the dictionary interface, it implements the map interface.
Next, look at the iterator. It is the tool that traverses the collection, that is, we usually traverse the collection through the iterator iterator. We say that collection relies on iterator because the collection implementation class implements the iterator () function and returns a iterator object.
The Listiterator is specifically designed to traverse the list.
Looking at enumeration, it is an abstract class introduced by JDK 1.0. function is the same as iterator, but the enumeration function is less than iterator. In the above block diagram, enumeration can only be used in Hashtable, vectors, and stack.
Finally, look at arrays and collections. They are two tool classes that manipulate arrays and collections.
With the overall framework above, we then analyze each class separately.

Collection Introduction
Let's look at the diagram of some of the framework classes in collection:

Collection is an interface, and its main two branches are: List and Set.
Both the list and set are interfaces, and they inherit from collection. The list is an ordered queue, and the list can have duplicate elements, and set is a collection of mathematical concepts, and there are no duplicate elements in the set!
Both the list and set have their respective implementation classes.
For convenience, we abstract the Abstractcollection abstract class, which implements most of the functions in the collection, so that in the collection implementation class, we can eliminate duplicate encoding through inheritance abstractcollection. Both Abstractlist and abstractset inherit from Abstractcollection, the specific list implementation class inherits from Abstractlist, and the set's implementation class inherits from Abstractset.
In addition, there is a iterator () function in the collection, which is the function of returning a iterator interface. Typically, we traverse the collection through the iterator iterator. The listiterator is specific to the list interface and returns a Listiterator object through Listiterator () in the list interface.
Next, we look at the introduction to each interface and abstract class, and then we have a detailed understanding of the implementation class.
The definition of collection is as follows:

Copy Code code as follows:

Public interface collection<e> extends iterable<e> {}

It is an interface, a highly abstracted collection that contains the basic operations of the collection: adding, deleting, emptying, traversing (reading), being empty, getting the size, protecting an element, and so on.

All subclasses of the collection interface (both direct subclasses and indirect subclasses) must implement 2 constructors: constructors with no parameters and constructors with parameters of collection. A constructor with parameters that can be used to convert a collection type.

Copy Code code as follows:

API for collection
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)
abstract int hashcode ()
Abstract Boolean IsEmpty ()
Abstract iterator<e> iterator ()
Abstract Boolean Remove (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 Introduction
The list is defined as follows:
Copy Code code as follows:

Public interface list<e> extends collection<e> {}

A list is an interface that inherits from collection, which is one of the collections. The list is an ordered queue, and each element in the list has an index; the index value of the first element is 0, and the index value of the subsequent element is +1. Unlike set, duplicate elements are allowed in the list.
The official description of the list is as follows:
A List is a collection which maintains of ordering for its elements. Every element in the List has a index. Each element can thus is accessed by its index being zero Normally, Lists allow duplicate elements, as compared to Sets, where elements have to be unique.


On the API side. Since the list is inherited from the collection interface, it naturally contains all the function interfaces in the collection, and since the list is an ordered queue, it also has its own API interface. There are mainly "add, delete, get, modify the element at the specified location", "Get the child queue in the list", and so on.
Copy Code code as follows:

API for collection
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)
abstract int hashcode ()
Abstract Boolean IsEmpty ()
Abstract iterator<e> iterator ()
Abstract Boolean Remove (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's 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 Introduction
The set is defined as follows:
Copy Code code as follows:

Public interface set<e> extends collection<e> {}

Set is an interface that inherits from collection, that is, set is also one of the collections. Set is a collection that has no duplicate elements.
On the API side. The set API is exactly the same as collection.
Copy Code code as follows:

Set's 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)
abstract int hashcode ()
Abstract Boolean IsEmpty ()
Abstract iterator<e> iterator ()
Abstract Boolean Remove (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 other than iterator () and size () in collection.
The main function of Abstractcollection: it implements most functions in the collection interface. In order to facilitate other classes to implement collection, such as ArrayList, LinkedList, and so on, they want to implement the collection interface, through the inheritance of Abstractcollection has achieved most of the interface.


5 Abstractlist
The definition of abstractlist is 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 functions other than size (), get (int location) in the list.
The main function of Abstractlist: it implements most of the functions in the list interface. So as to facilitate other classes to inherit list.
In addition, the iterator () interface is implemented in the Abstractlist abstract class compared to the abstractcollection.


6 Abstractset
The definition of Abstractset is 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. Because the set interface is exactly the same as the API in the collection interface, set does not have its own individual API. Like Abstractcollection, it implements functions other than iterator () and size () in the list.
The main function of Abstractset: it implements most of the functions in the set interface. So as to facilitate other classes to implement set interface.


7 iterator
The definition of iterator is as follows:
Copy Code code as follows:

Public interface Iterator<e> {}

Iterator is an interface that is an iterator of the collection. The collection can traverse the elements of the collection by iterator. The API interface provided by iterator includes: whether the next element exists, gets the next element, deletes the current element.
Note: When iterator traverse collection, it is the fail-fast mechanism. That is, when a thread a passes through iterator to traverse a collection, if the content of the collection is changed by another thread, then thread a accesses the collection, throws a Concurrentmodificationexception exception, and produces the Fail-fast event. We will explain the details of the fail-fast in the following sections. Todo
Copy Code code as follows:

API for iterator
Abstract Boolean Hasnext ()
Abstract E Next ()
abstract void Remove ()


8 Listiterator
The definition of Listiterator is as follows:
Copy Code code as follows:

Public interface listiterator<e> extends iterator<e> {}

Listiterator is an interface inherited from iterator, which is a queue iterator. Designed to facilitate the list, can provide forward/backward traversal. Compared to iterator, it adds an API interface, such as adding, having a previous element, getting the previous element, and so on.
Copy Code code as follows:

API for Listiterator
Interfaces 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.