Common features in the collection interface
1. Add
Boolean Add (Object obj);
Boolean AddAll (Collection); Adds all the elements in the specified container
2. Delete
void Clear ();
Boolean Removbe (object o);
Boolean RemoveAll (Collection c);
3. Get the length
int size ()
4. Judging
Boolean isEmpty ();
Boolean contains (object o)
Boolean contains (object)
5. Transfer a collection to a group
ToArray ();
ToArray ([]);
5. Remove the collection element iterator
Iterator Iterator ()
Gets an iterator object that iterates over the elements in the collection
Iteration: One way to remove an element
Iterators: Objects with iterative capabilities
The iterator object does not need new to be obtained directly through iterator ().
Iterators are public methods that take out collection collection elements
Packagecom.runoob.test;Importjava.util.ArrayList;Importjava.util.Collection; Public classCollectiondemo { Public Static voidMain (string[] args) {/** Demonstrates the basic features in collection **/Collection C1=NewArrayList (); Collection C2=NewArrayList (); /** Add a single element (object)*/C1.add ("ABC1"); C1.add ("ABC2"); C1.add ("ABC3"); C2.add ("ABC1"); C2.add ("ABC2"); C2.add ("ABC7"); //System.out.println ("C1 contains \" abc1\ "=" +c1.contains ("ABC1"));//result is true//System.out.println ("C1 contains \" abc\ "=" +c1.contains ("abc"));//The result is false//System.out.println (C1.remove ("ABC1"));//The result is true, which means that the deletion succeeded//System.out.println (C1.remove ("abc"));//The result is false, which means the delete failed//c1.clear ();//Empty C1//System.out.println (C1);//the result is []//System.out.println (C1.isempty ()); The result is true//C1.remove ("ABC1");//System.out.println (C1);//[ABC2,ABC3] intA=c1.size (); System.out.println (a); //====== with all method =======//C1.removeall (C2);//System.out.println (C1);//The result is [ABC3], deleting the same part of C1 and C2//C1.retainall (C2);//System.out.println (C1);//The result is [ABC1,ABC2], which returns the intersection of C1 and C2// //C1.addall (C2);//System.out.println (C1);//results for [ABC1, ABC2, ABC3, ABC1, ABC2, ABC7]//boolean b=c1.containsall (C2);//System.out.println (b);//result is true }}
A probe into the Java collection framework