Java learns 22nd from scratch (Set Interface), set from scratch
1. Definition of the Set interface is also a sub-interface of the Collection interface. However, unlike the Collection or List interface, duplicate elements cannot be added to the Set interface.
- The main methods of the Set interface are the same as those of the Collection interface.
- The instance of the Set interface cannot perform bidirectional output like the List interface. The get method does not exist and the Iterator interface is used to traverse the Set.
Two common methods: hasNext indicates to determine whether there are any elements that can be iterated.
Next () method: returns the next element of the iteration.
- Common subclass of the Set Interface
Hash Storage: HashSet
Ordered storage: TreeSet
Ii. Use SetHashSet
Package com. pb. demo2; import java. util. hashSet; import java. util. iterator; import java. util. set; import com. pb. demo2.Person; public class PersonSetTest {public static void main (String [] args) {/** create multiple Person objects and assign values to */Person p1 = new Person ("Zhang San ", 21); Person p2 = new Person ("Li Si", 22); Person p3 = new Person ("Wang Wu", 23); Person p4 = new Person ("Zhao Liu ", 24); Person p5 = new Person ("Qian Qi", 25); // create the Set interface object HashSet <Person> pset = new HashSet <Person> (); // use the add method to add pset. add (p1); pset. add (p2); pset. add (p3); pset. add (p4); pset. add (p5); // obtain the length of System. out. println ("Length:" + pset. size (); System. out. println ("============= use Iterator to traverse ========================== "); // because there is no get method, use Iterator to traverse Iterator <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 ("========= use foreach to traverse ================= "); // use foreach to traverse for (Person p: pset) {System. out. println ("name:" + p. getName (); System. out. println ("Age:" + p. getAge ());}}}3. Verify the storage of hash columns: HashSet
Package com. pb. demo2; import java. util. hashSet; import java. util. set; public class HashSetTest {public static void main (String [] args) {Set <String> allSet = new HashSet <String> (); allSet. add ("A"); // add the element allSet. add ("B"); // add the element allSet. add ("C"); // add the element allSet. add ("A"); // duplicate element. allSet cannot be added. add ("C"); // duplicate element. allSet cannot be added. add ("D"); // add the element allSet. add ("E"); // add the element System. out. println ("Length:" + allSet. size (); System. out. println (allSet. toString (); // output set object, call toString ()}}4. Verify the ordered storage: TreeSet
Package com. pb. demo2; import java. util. hashSet; import java. util. set; public class HashSetTest {public static void main (String [] args) {Set <String> allSet = new HashSet <String> (); allSet. add ("A"); // add the element allSet. add ("B"); // add the element allSet. add ("C"); // add the element allSet. add ("A"); // duplicate element. allSet cannot be added. add ("C"); // duplicate element. allSet cannot be added. add ("D"); // add the element allSet. add ("E"); // add the element System. out. println ("Length:" + allSet. size (); System. out. println (allSet. toString (); // output set object, call toString ()}}