Java Collection II (collection architecture)

Source: Internet
Author: User
Tags addall object object

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

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 collection, so that in collection's implementation class, We can omit the repeated 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

1 Collection Introduction

Collection is defined as follows:

Public interface collection<e> 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 apiabstract 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 Introduction

The list is defined as follows:

Public interface list<e> 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:

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.

 
Compared with collection,list 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

Set is defined as follows:

Public interface set<e> 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 of Apiabstract 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

Abstractcollection is defined 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.
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 Abstractlist<e> extends abstractcollection<e> 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 Abstractset<e> extends abstractcollection<e> 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 the iterator to traverse a collection, if the contents of the collection are changed by other threads, then thread a accesses the collection and throws a Concurrentmodificationexception exception. Generates a Fail-fast event . For more details on fail-fast, we will explain it in the following section. Todo

Iterator Apiabstract Boolean hasnext () abstract E next () abstract void Remove ()

8 Listiterator

Listiterator is defined as follows:

Public interface listiterator<e> 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 api//inherits from 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)

Java Collection II (collection architecture)

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.