Java Collection Interface

Source: Internet
Author: User
Tags addall

Java Collection centrally manages Dictionary, Vector, Stack, and Properties.
The interfaces supporting the class set are as follows:
Collection
List
Set
SortedSet
Comparator defines how to compare two objects
Objects in the Iterator enumeration class
Objects in the ListIterator enumeration class

Collection interface (Omitted common methods)
Boolean add (Object obj) add an Object element
Boolean addAll (Collection c)
Boolean contains (Object obj) determines whether obj is an element of the call class set (belongs)
Boolean containsAll (Collection c) determines whether c is a subset of the call class set (including)
Boolean equals (Collection c) determines whether c is equal to the call class set
Int hashCode () returns the hash code of the call class set.
Iterator iterator () returns the Iterator for calling the class set
Boolean removeAll (Collection c) removes all elements in c from the call class set (difference set)
Boolean retainAll (Collection c) Removes elements other than c from the call class set (Supplement set)
Object [] toArray () returns an array composed of elements of the Class Set
 
Void clear ()
Boolean isEmpty ()
Int size ()

The class set contains an add (Object obj) method, so it can contain any Object data, but cannot directly store data such as int, char, and double. You can use the following method:
ArrayList a = new ArrayList ();
A. add (new Integer (1 ));
A. add (new Integer (2 ));
......

When the class set cannot be modified, the UnsupportedOperationException may be thrown. ClassCastException may occur when an incompatible object is added to a class set.

List interface (inherited from Collection, using a 0-based subscript)
Void add (int index, Object obj)
Boolean addAll (int index, Collection c) returns true if the call list is changed. Otherwise, false is returned.
Object get (int index)
Int indexOf (Object obj) returns the index of the obj Object in the list.-1 is returned if no index exists.
Int lastIndexOf (Object obj) returns the subscript of the last instance of obj in the list.-1 is returned if no subscript exists.
ListIterator listIterator ()
ListIterator listIterator (int index) returns the iteration program starting with index.
Object set (int index, Object obj) modifies the value of the List index.
List subList (int start, int end) from start to end-1
Set interface (derived from Collection, no new method defined)
Set does not allow repeated elements. The Iterator () method is used to identify whether repeated elements are allowed.
Call the add (Object obj) method for Set. If obj already exists in the Set, false is returned.

SortedSet Interface
Comparator comparator () returns the comparison function that calls the sorting set. If you change the set to use the natural order, null is returned.
Object first () returns the first element of the sorted set.
SortedSet headSet (Object end) returns a SortedSet containing less than the end element.
Object last () returns the last element of the called sorting set.
SortedSet subSet (Object start, Object end) includes
SortedSet tailSet (Object start) returns an element greater than or equal to start.
ArrayList extends the AstractList class and executes the List interface. ArrayList supports dynamic length arrays.
LinkList extends AbstractSequentialList and executes the List interface. Provides a connection list.
HashSet extends AbstractSet to implement the Set interface, with no sequence of elements. Provides constant-level basic operations for large sets.
TreeSet uses the Set stored in the tree, and objects are stored in ascending order. Fast access and retrieval.

Iterator implements the Iterator interface or ListIterator interface.
Iterator Interface
Boolean hasNext ()
If Object next () does not have the next element, the NoSuchElementException exception is thrown.
Void remove () deletes the current element. If you try to call the remove () method after the next () method is called, The IllegalStateException exception is thrown.
ListIterator Interface
Void add (Object obj): before inserting an element into the current element, call the next () method to return this element.
Boolean hasNext ()
Boolean hasPrevious ()
Object next () triggers NoSuchElementException if it does not exist
Int nextIndex () if the returned list size does not exist
Void remove ()
Void set (Object obj) modifies the current element

Public void test1 (){
ArrayList al = new ArrayList ();
For (int I = 1; I <10; I ){
Al. add ("ArrayList Element:" I );
}
Iterator itr = al. listIterator ();
While (itr. hasNext ()){
Object obj = itr. next ();
System. out. println (obj );
}
}

Public void test2 (){
HashSet hs = new HashSet ();
System. out. println ("HashSet ");
For (int I = 1; I <10; I ){
Hs. add ("HashSet Element:" I );
}
Iterator itr = hs. iterator ();
While (itr. hasNext ()){
Object obj = itr. next ();
System. out. println (obj );
}
}

Public void test3 (){
TreeSet ts = new TreeSet ();
System. out. println ("TreeSet ");
For (int I = 1; I <10; I ){
Ts. add ("TreeSet Element:" I );
}
Iterator itr = ts. iterator ();
While (itr. hasNext ()){
Object obj = itr. next ();
System. out. println (obj );
}
}

Public void test4 ()
{
HashMap hm = new HashMap ();
For (int I = 0; I <10; I)
{
Hm. put ("item" I, "value" I );
}

Set set = hm. entrySet ();
Iterator itr = set. iterator ();
While (itr. hasNext ())
{
Map. Entry me = (Map. Entry) itr. next ();

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.