"Go" Java Collection series 02 Collection Architecture

Source: Internet
Author: User
Tags addall object object

Overview

First, we explain the collection. Let's take a look at the diagram of some of collection's framework classes:

Collection is an interface whose main two branches are:List and Set.

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 ().

Next, we look at the various interfaces and the introduction of the abstract classes, and then the implementation class for a detailed understanding.

The contents of this chapter include:
1 Collection Introduction
2 List Introduction
3 Set Introduction
4 abstractcollection
5 Abstractlist
6 Abstractset
7 Iterator
8 Listiterator

Reprint Please specify source: http://www.cnblogs.com/skywang12345/p/3308513.html

1 Collection Introduction

Collection is defined as follows:

 Public Interface extends iterable<e> {}

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


All subclasses of the collection interface (direct 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 the type of collection.

//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 ()
2 List Introduction

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.
List's official introduction is as follows :

A List is a collection which maintains an ordering for its elements. Every element in the List have an index.
Each element can thus is accessed by it index, with the first index being zero. Normally, Lists allow duplicate elements, as compared to sets, where elements has to be unique.

About the API aspect. Since list is inherited from the collection interface, it naturally contains all the function interfaces in collection, and since list is an ordered queue, it also has its own API interface. There are mainly "add, delete, get, modify elements at the specified location", "get sub-queues in list" and so on.

//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
3 Set Introduction

Set is defined as follows:

 Public Interface extends collection<e> {}

Set is an interface that inherits from collection, which is a set that is also one of the collections. Set is a collection of elements that have no duplicates.

About the API aspect. The set API is exactly the same as the collection.

//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 ()
4 abstractcollection

Abstractcollection is defined as follows:

 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.

5 Abstractlist

Abstractlist is defined as follows:

 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.

6 Abstractset

Abstractset is defined as follows:

 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.

7 Iterator

Iterator is defined as follows:

 Public Interface iterator<e> {}

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

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

8 Listiterator

Listiterator is defined as follows:

 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)

"Go" Java Collection series 02 Collection Architecture

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.