A probe into the Java collection framework

Source: Internet
Author: User
Tags addall

Common features in the collection interface
1. Add
Boolean Add (Object obj);
Boolean AddAll (Collection); Adds all the elements in the specified container

2. Delete
void Clear ();
Boolean Removbe (object o);
Boolean RemoveAll (Collection c);
3. Get the length
int size ()
4. Judging
Boolean isEmpty ();
Boolean contains (object o)
Boolean contains (object)
5. Transfer a collection to a group
ToArray ();
ToArray ([]);

5. Remove the collection element iterator
Iterator Iterator ()
Gets an iterator object that iterates over the elements in the collection
Iteration: One way to remove an element
Iterators: Objects with iterative capabilities
The iterator object does not need new to be obtained directly through iterator ().

Iterators are public methods that take out collection collection elements

 Packagecom.runoob.test;Importjava.util.ArrayList;Importjava.util.Collection; Public classCollectiondemo { Public Static voidMain (string[] args) {/** Demonstrates the basic features in collection **/Collection C1=NewArrayList (); Collection C2=NewArrayList (); /** Add a single element (object)*/C1.add ("ABC1"); C1.add ("ABC2"); C1.add ("ABC3"); C2.add ("ABC1"); C2.add ("ABC2"); C2.add ("ABC7"); //System.out.println ("C1 contains \" abc1\ "=" +c1.contains ("ABC1"));//result is true//System.out.println ("C1 contains \" abc\ "=" +c1.contains ("abc"));//The result is false//System.out.println (C1.remove ("ABC1"));//The result is true, which means that the deletion succeeded//System.out.println (C1.remove ("abc"));//The result is false, which means the delete failed//c1.clear ();//Empty C1//System.out.println (C1);//the result is []//System.out.println (C1.isempty ()); The result is true//C1.remove ("ABC1");//System.out.println (C1);//[ABC2,ABC3]                intA=c1.size ();        System.out.println (a); //====== with all method =======//C1.removeall (C2);//System.out.println (C1);//The result is [ABC3], deleting the same part of C1 and C2//C1.retainall (C2);//System.out.println (C1);//The result is [ABC1,ABC2], which returns the intersection of C1 and C2//    //C1.addall (C2);//System.out.println (C1);//results for [ABC1, ABC2, ABC3, ABC1, ABC2, ABC7]//boolean b=c1.containsall (C2);//System.out.println (b);//result is true                                    }}

A probe into the Java collection framework

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.