Common methods for the set interface in the collection interface in a collection class in Java are familiar with

Source: Internet
Author: User
Tags addall set set

The 1:set collection consists of the implementation classes of the set interface and the set interface, and the set interface inherits the collection interface because it contains all the methods of the collection interface.

2: Because duplicate values are not allowed in the set interface, you can use the AddAll () method in the Set collection to add the collection collection to the Set collection and remove the duplicate values

3: Case requirement, create a list collection object and add elements to the list collection. Create a set set, use the AddAll () method to save the list collection object to the Set collection and remove the duplicate values, and finally print the elements in the set set

1  Packagecom.ning;2 3 ImportJava.util.*;4 5  Public classDemo02 {6 7      Public Static voidMain (string[] args) {8         //TODO auto-generated Method Stub9List<string> list=NewArraylist<string> ();//Create a list collectionTenList.add ("B");//add an element to the list collection OneList.add ("a"); AList.add ("C"); -List.add ("Q"); -List.add ("C"); theSet<string> set=NewHashset<string> ();//To Create a list collection Object -Set.addall (list);//to add a list collection to the Set collection -Set.add ("111"); -Set.remove ("111"); +Iterator<string> It=set.iterator ();//Create a set iterator -SYSTEM.OUT.PRINTLN ("The element in the collection is:"); +          while(It.hasnext ()) { ASystem.out.print (It.next () + "\ T"); at         } -          -          -          -     } -  in}

1: To use a set collection, you typically declare it as a set type and then instantiate it through the set interface class. The implementation class of the set interface is commonly used in HashSet and TreeSet classes.

Set<string> set=new hashset<string> ();

Set<string> set=new treeset<string> ();

2: Because the objects in the collection are unordered, the result of traversing the set collection is not the same as the order in which the set is inserted

1  Packagecom.ning;2 3  Public classpeople {4     5     PrivateString name;6     Private LongId_card;7      PublicPeople (String name,LongId_card) {8          This. name=name;9          This. id_card=Id_card;Ten     } One      A      Public voidSetid_card (LongId_card) { -          This. id_card=Id_card; -     } the      Public LongGetid_card () { -         returnId_card; -     }  -      +      Public voidsetName (String name) { -          This. name=name; +     } A      PublicString GetName () { at         returnname; -     } -      -}
1  Packagecom.ning;2 3 ImportJava.util.*;4 5  Public classDemo05 {6 7      Public Static voidMain (string[] args) {8Set<people> set=NewHashset<people> ();//creating a Set Collection Object9Set.add (NewPeople ("Heart Grow Fonder", 10010));//adding elements to the collectionTenSet.add (NewPeople ("Xiao Li", 10011)); OneSet.add (NewPeople ("Xiao Zhao", 10012)); AIterator<people> It=set.iterator ();//Create a collection iterator -SYSTEM.OUT.PRINTLN ("The element in the collection is:"); -          while(It.hasnext ()) { thePeople p=It.next (); -System.out.println (P.getname () + "" +P.getid_card ()); -         } -     } +  -}

Common methods for the set interface in the collection interface in a collection class in Java are familiar with

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.