General usage of the Java--collection collection

Source: Internet
Author: User
Tags addall int size

The Collection interface is the parent interface of the list, Set, and queue interfaces, which are defined by methods that can be used to manipulate the set collection or to manipulate the list and queue collections.

The Collection interface defines the following methods for manipulating the set elements.

/* *  * boolean add (Object O): This method is used to add an element to the collection. Returns True * Boolean AddAll (Collection c) If the collection object was changed by the add Operation   : This method adds all the elements in the collection C to the specified collection. Returns true * void Clear () If the collection object is added to the table:   Clears all elements in the collection, changes the set length to 0  *   * Boolean contains (Object O): Returns whether the set contains a SET element  *   * Boolean containsall (Collection C): Returns whether the collection contains all elements in the set c *   * Boolean isEmpty (): Returns whether the collection is empty. Returns True if the collection length is 0 o'clock, otherwise false  * *   Iterator Iterator (): Returns a Iterator object that iterates through the elements in the collection  *   * Boolean remove ( Object o): Deletes the specified element in the collection O, when the collection contains one or more elements O, the method only deletes the first qualifying element, and the method returns True  *   * Boolean removeall (Collection C): Removes the element contained in the set C from the collection (equivalent to the set of calls to the method minus collection C), and if one or more of the above elements are deleted, the method returns True  *   * Boolean retainall (Collection C): Removes the element not contained in the collection from the collection (equivalent to the collection that invokes the method into the intersection of the collection and the set C), and if the operation changes the collection of calls to the method, the method returns True  *   * int size (): The method returns the number of elements in the collection  *   * object[] ToArray (): The method converts the set into an array, and all the collection elements become corresponding array elements  *   */

 Public Static voidMain (string[] args) {Collection C=NewArrayList (); //adding elementsC.add ("Monkey King"); //Although the base type value cannot be placed in the collection, Java supports automatic boxingC.add (1); Collection tion=NewArrayList ();  Tion.addall (c); //add elements of the C collection to the Tion collectionSystem. out. println (Tion.size ());//Output 2c.clear (); //clears all elements in the C collection//whether the tion collection contains "Monkey King"System. out. println (Tion.contains ("Monkey King"));//Output True//whether the Tion collection contains all the elements in the C collectionSystem. out. println (Tion.containsall (c));//Output True//determines whether the tion collection is emptySystem. out. println (Tion.isempty ());//Output False//delete the tion collection with the element "1"System. out. println (Tion.remove (1));//Output True//removes elements from the tion collection that are not contained in the C collectionSystem. out. println (Tion.remove (c));//Output False            }

General usage of the Java--collection collection

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.