Java Collection Learning Collection (1)

Source: Internet
Author: User
Tags addall

Java collection classes are mainly derived from two interfaces: collection and map, the two classes are the fundamental interface of the Java Collection Framework, the rest of the collection is the subclass of these two collections, this blog is mainly about the collection interface and its system contains set,list,queue and other collections. This is explained in turn.

I. Collection
1.Collection is the parent interface of the Set,list,queue interface, and the methods defined in collection are also applicable to Set,list,queue. The following code demonstrates the basic operation of the collection, as can be seen from the following code, where the operation of the collection is for an element and a collection, and the function names of the two operations are similar, except that for the operands the object is a collection, and the name of the functions is generally added to all.

Package Lkl;import Java.util.arraylist;import Java.util.collection;import java.util.HashSet; Public classcollectiontest { Public Static void Main(string[] args) {Collection c =NewArrayList (); Collection d=NewHashSet (); ///Add () method to add an element to a collection            ///AddAll () method to add a collectionC.add ("LKL"); D.add ("Test"); D.add ("Java"); C.addall (d); ///IsEmpty () method to test whether the collection is emptySystem. out. println ("is the collection empty?"+c.isempty ()); //Remove () to remove an element from the collection          ///   RemoveAll all elements of a collection can be removed from the current collectionC.remove ("LKL");           C.remove (d); System. out. println ("is the collection empty?"+c.isempty ()); C.add ("LKL"); C.add (d); ///contains () method can be used to determine whether a collection contains an element           ///  Containsall can be used to determine whether the collection contains all the elements in another collectionSystem. out. println ("Does the collection C contain" LKL "?"+c.contains ("LKL")); System. out. println ("is collection c all the elements in d?"+c.containsall (d)); ///Retainall () method to find the intersection of the current collection and the incoming collectionC.retainall (d); System. out. println (c); C.add ("LKL"); ///The size () method returns the number of elements in the collectionSystem. out. println ("The number of elements in set C is:"+c.size ());/*///toarray converts the collection to an array, the collection element becomes an array element string[] str = (string[]) C.toarray (); */           //Clear () clears the collectionC.clear (); System. out. println ("is the collection empty?"+c.isempty ()); }}

Traversal of the 2.Collection collection
We generally use iterator to iterate through the collection. Iterator is also a member of the Java Collection framework, which is used primarily to traverse the collection collection, which provides the following three methods:
BOOL Hasnext (): Returns True if the elements of the collection have not been traversed to play
Object Next (): Returns the next element in the collection
void Remove (): Deletes the element returned by the last Next () method in the collection
There are three places to be aware of:
1. The element returned from the collection is of type object, so we need to do the type conversion manually.
2. Each return is the value of the element in the collection rather than the element itself, so we cannot alter the elements themselves in the collection by altering the returned values.
3. We cannot change the elements in the collection during an iterative visit (but can call the iterator Remove method), otherwise an exception is thrown.
The following code demonstrates how to iterate through an element in a collection through an iterator:

Package Lkl;import Java.util.collection;import Java.util.hashset;import java.util.Iterator; Public classiteratortest { Public Static void Main(string[] args) {Collection c =NewHashSet (); C.add ("Java"); C.add ("C"); C.add ("C + +"); C.add ("Shell");///   Declare an collection iteratorIterator it = C.iterator (); while(It.hasnext ()) {///The   elements taken from the collection are of type object and require a type conversionString str = (string) it.next ();if(str=="Shell"){///   We can remove the iterator by calling the Remove method                ///   The element returned in the last iteration, but cannot change the elements in the collection in any other wayIt.remove ();///   The following code throws an exception                 ///C.add ("Python");} str="LKL";///   We cannot change the elements in the collection by changing the return value of the iterator} System. out. println (c); }}

Java5 can also iterate through the elements of the collection through a Foreach loop (just as with the elements in the array), using the Foreach method to access the note with the above iterator method, the following code demonstrates this method:

package  LKL; import  java.util.ArrayList; import  java.util.Collection; public  class  foreachtest  {  public  static  void  main  (string[] args) {Collection c =new  ArrayList (); C.add ( "java" ); C.add ( "C" ); C.add ( "C + +" ); C.add ( "shell" ); for  (Object cc:c) {String str = (string) cc; System.out.println (str); } }}

Java Collection Learning Collection (1)

Related Article

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.