Java starts from scratch 22 (set set interface)

Source: Internet
Author: User
Tags set set

The definition set interface of the set interface is also a sub-interface of the collection interface, but unlike the collection or list interface, duplicate elements cannot be added to the set interface.
    • The main method of the set interface is consistent with the collection
    • An instance of the set interface cannot be output bidirectional as the list interface does. There is no get method to traverse the collection using the iterator interface

2 Common methods: The Hasnext method indicates whether there are elements that can iterate

Next () Method: Returns the next element of the iteration

    • Common subclasses of the set interface

Hash Storage: HashSet

Ordered storage: TreeSet

Second, the use of Sethashset
 PackageCom.pb.demo2;ImportJava.util.HashSet;ImportJava.util.Iterator;ImportJava.util.Set;ImportCom.pb.demo2.Person; Public classPersonsettest { Public Static voidMain (string[] args) {/** Create multiple person objects and assign values*/Person P1=NewPerson ("Zhang San", 21); Person P2=NewPerson ("John Doe", 22); Person P3=NewPerson ("Harry", 23); Person P4=NewPerson ("Zhao Liu", 24); Person P5=NewPerson ("Money seven", 25); //Create a set interface object HashSetset<person>pset=NewHashset<person>(); //add by using the Add method shopPset.add (p1);         Pset.add (p2);         Pset.add (p3);         Pset.add (p4);         Pset.add (p5); //Get lengthSystem.out.println ("Length:" +pset.size ()); System.out.println ("=========== uses iterator to traverse ==============="); //because there is no get method, use iterator to traverseIterator<person> piterator=Pset.iterator (); //use the next and Hasnext methods to traverse         while(Piterator.hasnext ()) {person P=Piterator.next (); System.out.println ("Name:" +p.getname ()); System.out.println ("Age:" +p.getage ()); } System.out.println ("========= using foreach to traverse ============="); //use foreach to traverse         for(person P:pset) {System.out.println ("Name:" +p.getname ()); System.out.println ("Age:" +p.getage ()); }    }}
Third, verify the hash storage: HashSet
 PackageCom.pb.demo2;ImportJava.util.HashSet;ImportJava.util.Set; Public classHashsettest { Public Static voidMain (string[] args) {Set<String> Allset =NewHashset<string>(); Allset.add ("A");//add ElementAllset.add ("B");//add ElementAllset.add ("C");//add ElementAllset.add ("A");//duplicate element, cannot joinAllset.add ("C");//duplicate element, cannot joinAllset.add ("D");//add ElementAllset.add ("E");//add ElementSystem.out.println ("Length:" +allset.size ());        System.out.println (Allset.tostring ()); //output Collection Object, calling ToString ()    }}
Four, verify the orderly storage: TreeSet
 PackageCom.pb.demo2;ImportJava.util.HashSet;ImportJava.util.Set; Public classHashsettest { Public Static voidMain (string[] args) {Set<String> Allset =NewHashset<string>(); Allset.add ("A");//add ElementAllset.add ("B");//add ElementAllset.add ("C");//add ElementAllset.add ("A");//duplicate element, cannot joinAllset.add ("C");//duplicate element, cannot joinAllset.add ("D");//add ElementAllset.add ("E");//add ElementSystem.out.println ("Length:" +allset.size ());        System.out.println (Allset.tostring ()); //output Collection Object, calling ToString ()    }}

Java starts from scratch 22 (set set interface)

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.