Today saw collection Collection class This word, found that there is not much memory in mind, so re-review the collection interface. Then summarize the map interface later.
The following is the code of their own in the Myeclispe, only practice can improve the memory.
/**
*
The--collection interface is the parent interface of the set interface and the list interface, which is usually not used directly, but defines some common methods through which the basic operation of the collection can be implemented.
The common methods for collection interfaces are as follows:
1) Add (): Add an object to the collection
2) Remove (): Removes an object from the collection
3) IsEmpty (): Determines whether the current collection is empty
4) iterator (): Returns an iterator that iterates through the objects in the collection
5) Size (): Gets the number of elements in the collection
6) Clear (): Empty collection
7) contains (): Determines whether the specified object exists in the collection
8) AddAll (): Adds all the objects in the specified collection to the collection--
**/
Package com.fan.collection;
Import java.util.ArrayList;
Import java.util.Collection;
Import Java.util.Iterator;
public class Collectiondemo {
public static void Printcollectionelem (Collection<string> list) {
if (!list.isempty ()) {use of the//isempty () method
SYSTEM.OUT.PRINTLN ("The element in the collection is:");
Iterator<string> it= list.iterator ();use of//iterator ()
while (It.hasnext ()) {
System.out.println (It.next ());
}
}
}
public static void Main (String []args) {
String s1= "New China";
String s2= "New Thailand";
String s3= "Old China";
String s4= "Old China";
Collection<string> list=new arraylist<string> ();
List.add (S1);use of the//add method
List.add (S2);
List.add (S3);
List.add (S4);
List.add (S4);
Printcollectionelem (list);//Output The elements in list
System.out.println (List.size ());//size () method outputs the number of elements in the list
Collection<string> list2=new arraylist<string> ();
List2.addall (list);
Printcollectionelem (LIST2);
List2.remove (S1);
Printcollectionelem (LIST2);
//List.clear ();//clear () emptying the collection
//Printcollectionelem (list);
List2.removeall (LIST2);
Printcollectionelem (list);
System.out.println (List2.size ());
}
}
Collection interface Learning for Java collection classes