Tutorials to .com

Tutorials to .com » Jsp » Skills » java.util.Collection

java.util.Collection

Print View , by: iSee ,Total views: 21 ,Word Count: 1074 ,Date: Sun, 23 Aug 2009 Time: 4:06 AM

java Collection of Dictionary, Vector, Stack,, Properties for a unified management.
Cluster support for the interface is as follows:
Collection
List
Set
SortedSet
Comparator compare to the definition of two objects
Iterator object of enumeration type of focus
ListIterator enumerate categories focus on the subject of

Collection interface (commonly used method is omitted)
boolean add (Object obj) Add an Object element
boolean addAll (Collection c)
boolean contains (Object obj) is called to determine whether the type of obj set an element (of the)
boolean containsAll (Collection c) to determine whether c is called a subset of cluster (including)
boolean equals (Collection c) to determine whether c-type set to call the same
int hashCode () call to return to the hash cluster key
Iterator iterator () call to return to the iterative procedure Cluster
boolean removeAll (Collection c) remove from the call type of focus to include all elements of C++ (Difference Sets)
boolean retainAll (Collection c) remove from the call included in the category focus on other than the elements in c (complement set)
Object [] toArray () to return to the cluster of the array elements

void clear ()
boolean isEmpty ()
int size ()

Category contains a set add (Object obj) method, so the data can contain any Object, but can not be stored directly: int, char, double, etc.. Can use the following method:
ArrayList a = new ArrayList ();
a.add (new Integer (1));
a.add (new Integer (2));
... ...

When the cluster can not be amended, may lead to abnormal UnsupportedOperationException. Attempt to a non-compatible objects into focus when a class may lead to abnormal ClassCastException.

List interface (inherited from the Collection, using the subscript 0)
void add (int index, Object obj) Insert an element after the post-shift
boolean addAll (int index, Collection c) If the call list has changed, return true, otherwise return false
Object get (int index)
int indexOf (Object obj) return obj object index in the list, there is no return -1
int lastIndexOf (Object obj) return obj in the list of the last examples of the subscript does not exist return -1
ListIterator listIterator ()
ListIterator listIterator (int index) return index of the iterative process to start
Object set (int index, Object obj) on the list of index values to amend Department
List subList (int start, int end) from the start to the end-1


Set interface (derived from the Collection, there is no definition of the new method)
Set elements are not permitted to repeat.
Set calls to add (Object obj) method, if obj already exists in the collection, will return false.

SortedSet Interface
Comparator comparator () call to return to the comparison function to sort the collection, if the collection of the use of the natural order, then return null
Object first () to return the collection to be sort of the first element
SortedSet headSet (Object end) returns an element that contains less than the end of the SortedSet
Object last () to return to the last call to sort a set of elements
SortedSet subSet (Object start, Object end) from the start to the end-1
SortedSet tailSet (Object start) return contains elements greater than or equal to start


ArrayList Class AstractList expansion and the implementation of List interface. The length of the ArrayList supports dynamic arrays.
LinkList expanded AbstractSequentialList, the implementation of List interface. Provide connectivity list.
Set to achieve AbstractSet expansion HashSet interface, not the order of elements. Constant for large collections to provide the basic operation level.
Use TreeSet to store the tree of Set, the object storage according to ascending. Very fast access and retrieval.

iterator achieve Iterator or ListIterator interfaces interface.
Iterator interface
boolean hasNext ()
Object next () if there is no element of the next trigger abnormal NoSuchElementException.
void remove () to delete the current element, if the attempt to call next () method called remove () method will lead to abnormal IllegalStateException.



ListIterator Interface
void add (Object obj) Insert an element before the current element, call next () method will return the element.
boolean hasNext ()
boolean hasPrevious ()
Object next () If there is no trigger NoSuchElementException
int nextIndex () If there is no return to the list of the size of
void remove ()
void set (Object obj) to amend 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 ();




Java skills and code Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.