Collection interface Learning for Java collection classes

Source: Internet
Author: User

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

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.