Collection in java

Source: Internet
Author: User

 

Set features: Containers used to store objects (reference of stored objects). The length of the set is variable, and basic data type values cannot be stored in the set.

      

 

Differences between a set and an array:

Can store any object in the collection, and the length is variable

The array can only store data of the same type, and the length is immutable.

 

Common methods of Collection:

Add element boolean add (Object );

        ArrayList a1 =      a1.add("java01"     a1.add("java02"     a1.add("java03"     a1.add("java04"     System.out.println(a1); }

Delete the boolean remove (Object) element. The returned value is boolean. (You can delete an Object using the specified badge in the List set. The returned value is the deleted Object)

        ArrayList a1 =      a1.add("java01"     a1.add("java02"     a1.add("java03"     a1.add("java04"      b = a1.remove("java03"   }

Clear element: void clear (); this exception is thrown if the set does not support this method: UnsupportedOperationException

        ArrayList a1 =      a1.add("java01"     a1.add("java02"     a1.add("java03"     a1.add("java04"   }

Determine whether an element exists: boolean contains (Object)

ArrayList a1 = a1.add ("java01" a1.add ("java02" a1.add ("java03" a1.add ("java04" B = a1.contains ("java03" System. out. println ("Does java03 exist:" +}

Get the number of elements in the Set: int size ();

    ArrayList a1 =      a1.add("java01"     a1.add("java02"     a1.add("java03"     a1.add("java04"      a =     System.out.println("size:"+ }

Determines whether the set is empty: boolean isEmpty ();

ArrayList a1 = a1.add ("java01" a1.add ("java02" a1.add ("java03" a1.add ("java04" B = System. out. println ("is the set empty? : "+}

Intersection: al1 retains only the same elements as al2. if there is no intersection, It is null: boolean retainAll (Object)

        ArrayList al1 =      al1.add("java01"     al1.add("java02"     al1.add("java03"     al1.add("java04"              ArrayList al2 =      al2.add("java01"     al2.add("java02"     al2.add("java05"     al2.add("java06"                                             b = al1.removeAll(al2);   }

 

 

Special method Iterator: retrieves the element in the Set: iterator <e> Iterator (); (an Iterator interface is returned)

Methods In the Iterator interface:
There are no elements to judge: boolean hasNext ()
Next element of iteration: E next ();
Delete element: void remove ()

        ArrayList al =      al.add("java01"     al.add("java02"     al.add("java03"     al.add("java04"                      Iterator it =                        }

 

 

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.