General framework of Java Collection series 01

Source: Internet
Author: User

Many times before, very troublesome, record down.

The following summary from HTTP://WWW.CNBLOGS.COM/SKYWANG12345/, reprint to pay attention!

Overall framework

The trunk is collection and map.

1 (a) list is an ordered queue, and each element has its index. The index value of the first element is 0.
The list implementation class has LinkedList, ArrayList, Vector, Stack.

Set is a collection that does not allow repeating elements.
The implementation class for set has Hastset and TreeSet. HashSet relies on HashMap, which is actually implemented through HashMap; TreeSet relies on treemap, which is actually implemented by TreeMap.

2 map is a mapping interface, which is a Key-value key-value pair. Each element in the map contains "one key" and "key corresponding to 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 dictionary, it implements the map interface.

Collection

Both the list and the set are interfaces that inherit from the collection. list is an ordered queue , the list can have duplicate elements , and set is a set of mathematical concepts, set does not have duplicate elements !
Both the list and the set have their own implementation classes.

For convenience, we abstracted the Abstractcollection abstract class, which implements most of the functions in the collection, so that in collection's implementation class, we can omit the repetitive encoding by inheriting abstractcollection. Both Abstractlist and abstractset inherit from the Abstractcollection, and the specific list implementation class inherits from the Abstractlist, and the set implementation class inherits from the Abstractset.

In addition, there is a iterator () function in collection that returns a iterator interface. Typically, we iterate through the collection through the iterator iterator. Listiterator is unique to the list interface, and in the list interface, a Listiterator object is returned through Listiterator ().

Collection API

//Collection's APIAbstract BooleanAdd (E object)Abstract BooleanAddAll (collection<?extendsE>collection)Abstract voidClear ()Abstract Booleancontains (Object object)Abstract BooleanContainsall (collection<?>collection)Abstract Booleanequals (Object object)Abstract inthashcode ()Abstract BooleanIsEmpty ()AbstractIterator<e>iterator ()Abstract Booleanremove (Object object)Abstract BooleanRemoveAll (collection<?>collection)Abstract BooleanRetainall (collection<?>collection)Abstract intsize ()Abstract<T>t[] ToArray (t[] array)AbstractObject[] ToArray ()

The list is defined as follows

 Public Interface extends collection<e> {}

List is an interface that inherits from collection, which is a list that is one of the collections. List is an ordered queue, 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.

//Collection's APIAbstract BooleanAdd (E object)Abstract BooleanAddAll (collection<?extendsE>collection)Abstract voidClear ()Abstract Booleancontains (Object object)Abstract BooleanContainsall (collection<?>collection)Abstract Booleanequals (Object object)Abstract inthashcode ()Abstract BooleanIsEmpty ()AbstractIterator<e>iterator ()Abstract Booleanremove (Object object)Abstract BooleanRemoveAll (collection<?>collection)Abstract BooleanRetainall (collection<?>collection)Abstract intsize ()Abstract<T>t[] ToArray (t[] array)Abstractobject[] ToArray ()//compared to the new API with Collection,list:Abstract voidAddintLocation , E object)Abstract BooleanAddAll (intLocation, collection<?extendsE>collection)AbstractE Get (intLocation )Abstract intindexOf (Object object)Abstract intlastIndexOf (Object object)AbstractListiterator<e> Listiterator (intLocation )AbstractListiterator<e>Listiterator ()AbstractE Remove (intLocation )AbstractE Set (intLocation , E object)AbstractList<e> sublist (intStartintEnd

Set definition, set API and collection exactly the same.

//Set's APIAbstract BooleanAdd (E object)Abstract BooleanAddAll (collection<?extendsE>collection)Abstract voidClear ()Abstract Booleancontains (Object object)Abstract BooleanContainsall (collection<?>collection)Abstract Booleanequals (Object object)Abstract inthashcode ()Abstract BooleanIsEmpty ()AbstractIterator<e>iterator ()Abstract Booleanremove (Object object)Abstract BooleanRemoveAll (collection<?>collection)Abstract BooleanRetainall (collection<?>collection)Abstract intsize ()Abstract<T>t[] ToArray (t[] array)AbstractObject[] ToArray ()

Abstractcollection definition

 Public Abstract class Implements collection<e> {}

Abstractcollection is an abstract class that implements functions other than iterator () and size () in collection.
Abstractcollection's main role: it implements most of the functions in the collection interface. In order to facilitate the implementation of other classes collection, such as ArrayList, LinkedList, and so on, they want to implement the collection interface, through inheritance Abstractcollection has implemented most of the interface.

Definition of Abstractlist

 Public Abstract class extends Implements list<e> {}

Abstractlist is an abstract class that inherits from the Abstractcollection and implements the list interface. It implements a function other than size (), get (int location) in the list.
Abstractlist's main function: it implements most of the functions in the list interface. This facilitates other classes to inherit the list.
In addition, the iterator () interface is implemented in the Abstractlist abstract class compared to abstractcollection.

Definition of Abstractset

 Public Abstract class extends Implements set<e> {}

Abstractset is an abstract class that inherits from the 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 separate API. Like Abstractcollection, it implements functions other than iterator () and size () in the list.
Abstractset's main function: it implements most of the functions in the set interface. Thus, it is convenient for other classes to implement the set interface.

iterator Definition
 Public Interface iterator<e> {}

// Iterator's API Abstract Boolean Hasnext () Abstract E Next () Abstract void Remove ()

Iterator is an interface that is an iterator to a collection. The collection can traverse the elements in the collection through iterator. Iterator provides an API interface that includes whether the next element exists, gets the next element, and deletes the current element.
Note: Iterator is a fail-fast mechanism when traversing collection. That is, when a thread a passes through iterator to traverse a collection, if the contents of that collection are changed by other threads, thread a accesses the collection and throws an Concurrentmodificationexception exception, resulting in a fail-fast event. For more details on fail-fast, we will explain it in the following section. Todo

Listiterator definition

 Public Interface extends iterator<e> {}

Listiterator is an interface that inherits from iterator, which is a queue iterator. Designed to facilitate the list, providing forward/backward traversal. Compared to iterator, it adds API interfaces for adding, having a previous element, getting the previous element, and so on.

//Listiterator's API//interfaces inherited from the iteratorAbstract BooleanHasnext ()AbstractE Next ()Abstract voidRemove ()//New API InterfaceAbstract voidAdd (E object)Abstract Booleanhasprevious ()Abstract intNextindex ()AbstractE Previous ()Abstract intPreviousindex ()Abstract voidSet (E object)

(RPM) General framework of Java Collection series 01

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.