Java Collection Collection method

Source: Internet
Author: User
Tags addall

    • First, the Simple method
 Packagecn.itcast.day15;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.Collection;/** * The origin of the collection: * We are learning object-oriented language, and object-oriented language description of things is through the object, in order to facilitate the operation of multiple objects, we have to store these multiple objects. * and to store multiple objects, it cannot be a basic variable, but a container object, what are the container types in the knowledge we are currently learning? * Arrays and StringBuffer. But what? The result of StringBuffer is a string that does not necessarily meet our requirements, so we can only select an array, which is an array of objects. * and the object array does not adapt to the changing needs, because the length of the array is fixed, this time, in order to adapt to the needs of change, Java provides a collection of types for us to use. * * Array and set differences: * A: Length difference * Fixed length of array * B: Different contents * Array stores a type of element * While storing different classes in the collection Type of element * C: The data type of the element is different * array can store the base type, or you can store reference type * Collection can only store reference type * * Just said that the collection is stored multiple elements, but we also have to store multiple elements With demand: For example, I want to have the same elements in many of these elements. * Again, for example, I want these elements to be sorted by some sort of rule. Java provides a variety of collection classes for different requirements, so Java provides a number of collection classes. * The data structure of these multiple collection classes is different, the structure is not important, it is important to be able to store things, but also to be able to use these things, such as judgment, access and so on. * In this case, then, these multiple collection classes are common content, we put the common content of these collection classes continue to draw upward, and finally can form a set of inheritance architecture. * * Data structure: How it is stored. * * Collection: The top interface of the world's European stocks, its sub-system has a duplicate, there is only, orderly, there is disorder. (Later on) * * Collection features Overview: * 1: Add function * Boolean Add (E): Add an element to the collection * Boolean AddAll (Collectio N<?         Extends e> c): Add a collection to the collection * 2: Delete function * void clear (): Remove all elements from the collection * Boolean remove (Object o) removes an element from the collection * Boolean RemoveAll (collection<?> c): Removes the collection element specified in the collection * 3: Judgment Function * Boolean contains (Object O): Determines whether the collection contains a reference Fixed Element * Boolean Containsall (collection<?> C): Determines whether the collection contains all elements in the specified collection * Boolean isEmpty (): Determines whether the collection is empty * 4        : Get function * iterator<e> Iterator (): Iterator returning iteration set (FOCUS) * 5: Length function * int size (): Length of collection * 6: Intersection function * Boolean retainall (collection<?> C): Two elements of the set * 7: Conversion function * object[] ToArray (): Converts a collection to an array of object objects---and Arrays st          atic <T> list<t> aslist (T ... a) corresponds to **/ Public classCollectiondemo { Public Static voidMain (string[] args) {Collection C=NewArrayList (); //boolean Add (E): Adds an element to the collectionC.add ("ABC1"); C.add ("ABC2"); C.add ("ABC3"); C.add ("ABC4"); //void Clear (): Removes all elements from the collection//c.clear (); //boolean remove (Object o) removes an element from the collection//c.remove ("Abc5"); //Boolean contains (Object O): Determines whether the collection contains the specified element//System.out.println (C.contains ("123"));//flase//System.out.println (C.contains ("ABC2"));//true//boolean isEmpty (): Determines whether the collection is empty//System.out.println ("IsEmpty:" +c.isempty ()); //int Size (): The length of the collection//System.out.println ("Size ():" +c.size ());//4//C.remove ("ABC1");//System.out.println ("Size ():" +c.size ());//3object[] Objs=C.toarray ();  for(inti=0;i<objs.length;i++) {Object obj=Objs[i];            System.out.println (obj); } C=arrays.aslist (OBJS); System.out.println ("C:" +c); }}
    • Ii. Advanced Methods
 Packagecn.itcast.day15;Importjava.util.ArrayList;Importjava.util.Collection;/*** * * * Collection Collection advanced Function Test * Xxall Method Test * Boolean addall (collection<? extends e> c): Adds a collection to the collection * Boolean remove All (collection<?> c): Removes the collection element specified in the collection * Boolean Containsall (collection<?> C): Determines whether all elements in the specified collection are contained in the collection * Boolean Retainall (collection<?> C): Two elements returned by the collection : Returns True if this Collection is changed because of a call  */ Public classCollectionDemo2 { Public Static voidMain (string[] args) {Collection C1=NewArrayList (); C1.add ("ABC1"); C1.add ("ABC2"); C1.add ("ABC3"); C1.add ("ABC4"); Collection C2=NewArrayList (); C2.add ("ABC1"); C2.add ("ABC2"); C2.add ("ABC3"); C2.add ("ABC4"); C2.add ("Abc5"); C2.add ("Abc6"); C2.add ("ABC7"); //C1.addall (C2); //System.out.println (C1.removeall (C2)); //System.out.println (C1.containsall (C2));//System.out.println (C1.retainall (C2));System.out.println (C1);    System.out.println (C2); } }

Java Collection Collection method

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.