Summary of collection collection types in Java _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).
The Java Collection Toolkit framework is shown below.

Description: Look at the frame above, first grab its trunk, that is, collection and map. The
collection is an interface, a highly abstracted collection that contains the basic operations and attributes of a collection. The
collection contains two large branches of list and set. The
list is an ordered queue, and each element has its index. The index value of the first element is 0. The implementation class for the
list has LinkedList, ArrayList, Vector, Stack. The
() set is a collection that does not allow duplicate elements. The implementation class for the
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
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". The
Abstractmap is an abstract class that implements most of the APIs in the map interface. And Hashmap,treemap,weakhashmap are inherited from Abstractmap. The
Hashtable, although inherited from dictionary, 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 exists specifically to traverse the list.
Look at the enumeration, which 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. After the
has the overall framework above, we then analyze each class separately.

Collection architecture
below, we will summarize the collection. 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.
To facilitate implementation, the Abstractcollection abstract class is defined in the set, which implements most of the functions in the collection, so that in the collection implementation class, We can omit duplicate encoding by inheriting 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.

1. Collection Introduction
the definition of collection is 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.

Collection API
Abstract Boolean   Add (E object)
Abstract Boolean   addall (collection<? extends E > collection) abstract
void Clear   ()
Abstract Boolean   contains (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:

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.

Collection API Abstract Boolean Add (E object) Abstract Boolean addall (collection<? extends e> Collection) AB stract void Clear () Abstract Boolean contains (object) Abstract Boolean containsall (collection<?> Collec tion abstract Boolean equals (object) abstract int hashcode () Abstract Boolean isempty () abstract ITERATOR&L T E> iterator () Abstract Boolean Remove (Object object) Abstract Boolean RemoveAll (collection<?> Collection) AB Stract boolean retainall (collection<?> Collection) abstract int size () abstract <T> t[] ToArray (t[) arr ay) abstract object[] ToArray ()//vs Collection,list new api:abstract void Add (int location, E Object) abstract Boolea n AddAll (int location, collection<? extends e> Collection) abstract E get (int location) Abstract int Inde
XOf (Object object) abstract int LastIndexOf (object) abstract listiterator<e> listiterator (int location) Abstract listiterator&Lt E> Listiterator () abstract e-Remove (int location) abstract e set (int location, E object) abstract List<e&gt    ;
 sublist (int start, int end)

3. Set Introduction
the set is defined 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.

The Set API
abstract Boolean   Add (E object)
Abstract Boolean   addall (collection<? extends e> Collection) abstract
void Clear    ()
Abstract Boolean   contains (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

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. 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:

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. As for the details of Fail-fast, we will explain them specifically after the Fail-fast summary.

Iterator API
Abstract Boolean hasnext () abstract
E next ()
abstract void Remove ()

8. Listiterator
the definition of Listiterator is 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.

Listiterator API
//Iterator Interface
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 Article

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.