Java learns 22nd from scratch (Set Interface), set from scratch

Source: Internet
Author: User

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 ()}}

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.