3 Examples of Java collection collection

Source: Internet
Author: User
Tags addall

Package CN. Itcast_01;Import Java. Util. ArrayList;Import Java. 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. * If you want to store multiple objects, you can't be a basic variable, but a variable of a container type, what are the container types in the knowledge we've learned so far? * 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 needs of change, because the length of the array is fixed, this time, in order to adapt to the needs of change, Java provides a collection of classes for us to use. * * What is the difference between arrays and collections?      * A: Length difference * Fixed length of array * Set length variable * B: Content different * Array stores elements of the same type * and collections can store different types of elements * C: Problem with the data type of the element * Arrays can store the base data type, or you can store reference data types * Collections can only store reference types * * Just said the collection is a multi-element store, but we also have different needs to store multiple elements: for example, I want these multiple The element cannot have the same elements, * again, 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 upward extraction, and finally can form a set of inheritance architecture. * * Data structure: How it is stored. * * Collection: Is the top-level interface of the set, its sub-system has the repetition, has the unique, has the orderly, has the disorder. (will be explained slowly later) * * Collection features Overview: * 1: Add Features * Boolean add (Object obj): Add an element * Boolean AddAll (Collection C): Add a Collection  Element * 2: Delete function * void clear (): Remove all elements *    Boolean remove (Object o): Removes an element * Boolean RemoveAll (Collection c): Removes the element of a collection (is one or all) * 3: Judging function * Boolean cont Ains (Object O): Determines whether the specified element is contained in the collection * Boolean Containsall (Collection C): Determines whether the collection contains the specified collection element (is one or all) * Boolean isEmpty (): Determine if the collection is empty * 4: Get function * iterator<e> Iterator () (Key) * 5: Length function * int size (): Number of Elements * interview: Does the array have the length () method? string Is there a length () method? Does the collection have the length () method? * 6: Intersection function * Boolean retainall (Collection C): Two elements of the set? Where is the thought element, and what does the Boolean return mean? * 7: Convert set to Array * object[] toArray () */public class Collectiondemo {public static void main (string[] args) {//Test without All method//Create collection Object Collection C = new Collection ();//error because the interface cannot be instantiatedCollection C = new ArrayList ();BooleanAdd(Object obj): Add an element//System. out. println("Add:"+c. Add("Hello"));C. Add("Hello");C. Add("World");C. Add("Java");void Clear (): Remove all elements//C. Clear();Boolean remove (Object o): Remove an element//System. out. println("Remove:"+ C. Remove("Hello"));System. out. println("Remove:"+ C. Remove("Java ee"));Boolean contains (Object O): Determines whether the collection contains the specified element//System. out. println("contains:"+c. Contains("Hello"));System. out. println("contains:"+c. Contains("Android"));Boolean IsEmpty (): Determines whether the collection is empty//System. out. println("IsEmpty:"+c. IsEmpty());int size (): Number of elements System. out. println("Size:"+c. Size());System. out. println("C:"+ c);}}
Package CN. Itcast_01;Import Java. Util. ArrayList;Import Java. Util. Collection;/ * Boolean addall (Collection C): Adds an element of a collection * Boolean RemoveAll (Collection c): Removes the element of a collection (is one or all) * Boolean containsal L (Collection C): Determines whether the collection contains the specified set element (is one or all) * Boolean retainall (Collection C): Two elements of a collection? Where is the thought element, and what does the Boolean return mean? */public class CollectionDemo2 {public static void main (string[] args) {//Create collection1Collection C1 = new ArrayList ();C1. Add("ABC1");C1. Add("ABC2");C1. Add("ABC3");C1. Add("ABC4");Create a Collection2Collection C2 = new ArrayList ();C2. Add("ABC1");C2. Add("ABC2");C2. Add("ABC3");C2. Add("ABC4");C2. Add("Abc5");C2. Add("ABC6");C2. Add("ABC7");Boolean AddAll (Collection C): Add an element of a collection//System. out. println("AddAll:"+ C1. AddAll(C2));Boolean RemoveAll (Collection c): Removes the element of a collection (one or all)//returns True whenever an element is removed. System. out. println("RemoveAll:"+c1. RemoveAll(C2));Boolean containsall (Collection C): Determines whether the collection contains the specified collection element (is one or all)//only contains all elements, called include//System. out. println("Containsall:"+c1. Containsall(C2));Boolean retainall (Collection C): Two elements of a collection? Where does the thought element go, and what does the Boolean return mean?        Suppose there are two sets, A, B.        A to B intersection, the final result is saved in a, B unchanged.        The return value indicates whether a has changed. System. out. println("Retainall:"+c1. Retainall(C2));System. out. println("C1:"+ C1);System. out. println("C2:"+ C2);}}
 Packagecn.itcast_01;ImportJava.util.ArrayList;ImportJava.util.Collection;/ * Traversal of the collection. It is actually getting each element in the collection in turn. * * object[] ToArray (): Sets the set to the array, you can implement the collection of traversal */ Public  class CollectionDemo3 {     Public Static void Main(string[] args) {//Create collection ObjectCollection C =NewArrayList ();//Add elementC.add ("Hello");//Object obj = "Hello";C.add ("World"); C.add ("Java");//Traversal        //object[] ToArray (): Sets the set to the array, you can implement the collection of traversalobject[] Objs = C.toarray (); for(intx =0; x < objs.length; X + +) {//System.out.println (objs[x]);            //I know that the element is a string, and I want to know the length of the element while I get the element.             //System.out.println (Objs[x] + "---" + objs[x].length ());            //above cannot be implemented because there is no length () method in Object            //If we want to use a string method, we must restore the element to a string            //Down Transformationstring s = (string) objs[x]; SYSTEM.OUT.PRINTLN (S +"---"+ s.length ()); }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

3 Examples of 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.