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)