JAVA Container Class Summary __java

Source: Internet
Author: User
Tags int size
Original source: http://www.cnblogs.com/wishyouhappy/p/3669198.html 1.java container Classification diagram

Description: The first diagram is a simplified diagram (where the thick line is the focus of the container), and the second diagram is a complete container classification diagram

2. Container class interface and abstract container class

2.1 Description

The container interface is the base of the container. Using an interface allows you to separate the implementation of a container from the container interface, so you can use the same method to access the container without caring for the container's specific data structure.

Similarly, the iterator interface enables users to access different container classes using the same method.

2.2 Container Interface (Collection,map,iterator)

1) Collection Interface

* Boolean Add (Object obj): Adding objects, changing the collection returns True
* Iterator iterator (): Returns the object of the iterator interface
* int size ()
* Boolean IsEmpty ()
* Boolean contains (Object obj)
* void Clear ()
* <T> t[] ToArray (t[] a)

2 The Map interface (holds the key value pair, the value in the map can also be a container)

* Object get (Object key)
* Object put (object key, Object value)
* Set keyset (): Returns the keys set      Set<k> keyset ()
* Set EntrySet (): Returns mappings set    set<map.entry<k,v>> entryset ()
* ContainsKey ()

3) Iterator interface

* Object Next ()
* Boolean hasnext ()
* void Remove ()

Note: The Remove function cannot be executed repeatedly, otherwise it returns illegalstateexception

(If the next method has not yet been called, or the Remove method has already been called method.)

Usual usage:

Iterator It=collection.iterator ();
while (It.hasnext ())
{
Object obj=it.next ();
 Do something 
}

2.3 Sub-Interface (List,set,listiterator,sortedmap,sortedset)

1 List (the order can be repeated, in order to do so when the operation can be added to the method index parameters, as follows:)

* Boolean Add (E Element)

* e Set (int index, E Element)
* E get (int index);

2 Set (no order can not be repeated, unordered and therefore can not be indexed to manipulate objects)

3) Listiterator (iterator for list,list is a two-way table, thus adding some new methods on iterator, allowing the traverse the List in either direction)

* Boolean hasprevious ();
* E Previous ();
* int Previousindex ()

4) SortedMap

Description: Guarantees that the mappings in ascending order of the keys can be sorted according to the natural sequence of the keys (comparable interface) or by the comparer provided by creating an ordered map
(a Map that is further provides a total ordering in its keys. The map is ordered according to the natural ordering of the IT keys, or by a Comparator typically-provided at sorted map Crea tion time)
public interface Sortedmap<k,v>extends map<k,v>

* Comparator Comparator ()
* Object Firstkey ()
* Object Lastkey ()

5) SortedSet

is primarily used for sorting operations, and subclasses that implement this interface are sorted subclasses

public interface Sortedset<e>extends set<e>
* Comparator Comparator ()
* E First (): Return to Element one

* sortedset<e> Headset (E toelement): Return less than toelement
* sortedset<e> Tailset (E fromelement)
*

2.4 Abstract Container Class

1 Description: The use of abstract container classes can facilitate the definition of classes, without having to implement all the methods in the container interface container in each class

2) contains:

   * Abstractcollection Public  abstract class Abstractcollection<e>extends objectimplements collection<e&  gt;
 * Abstractlist Public              abstract class Abstractlist<e>extends abstractcollection<e>implements List< E>
 * Abstractset public  abstract class Abstractset<e>extends abstractcollection<e> Implements set<e>
* Abstactmap public                abstract class Abstractmap<k,v>extends Object implements Map <K,V>  * abstractsequentiallist public    abstract class Abstractsequentiallist<e> extends Abstractlist<e>       

3. Specific Container class

3.1 Overview

1) Collection:arraylist,linkedlsit,vector,stack

Treeset,hashset,linkedhashset

2 Map:hashmap,linkedhashmap,weakhashmap, TreeMap, HashTable, identityhashtable (where key comparisons are via = = rather than equals)

3.2 Common Container classes

1 ArrayList and LinkedList (both asynchronous, multithreading needs to consider thread safety issues), Vector (sync), Stack

1. The List interface supports indexing methods to access elements: ArrayList Random access quickly change slowly; LinkedList fast random access slow; vectors achieve synchronization, thus slower than ArrayList

2. LinkedList uses a two-way linked list to implement LinkedList to provide additional Get,remove,insert methods at LinkedList's header or tail. These operations enable LinkedList to be used as stacks (stack), queues (queue), or bidirectional queues (deque).

3. ArrayList does not define the growth algorithm, when you need to insert a large number of elements is, you can invoke the Ensurecapacity method to improve the efficiency of adding

4. Vectors are similar to ArrayList, but are synchronized, multi-threaded security (another difference is that the ArrayList increases by half by default and the vector grows by one fold). Vector is slower than ArrayList, either single-threaded or multi-threaded

5. Stack inherits from Vector, implements a LIFO stack

6. If you need to implement synchronization, you can invoke the Synchronizedlist method of the Collections tool class, as follows:

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.