The Way to Java--Day19(set interface)

set interface

The java.util.Set interface, like the java.util.List interface, also inherits from the Collection interface, which is basically the same as the method in the Collection interface, but the elements in the set interface are out of order and do not repeat

- Classification

1.HashSet Collection

2.LinkedHashSet Collection

HashSet collection:

The java.util.HashSet is an implementation class of the Set interface, which stores elements that are not repeatable and that are out of order (i.e., access order is inconsistent)

 1 package demosummary.set;
 2 
 3 import java.util.HashSet;
 4 
 5 public class HashSetTest01 {
 6     public static void main(String[] args) {
 7         //Establish set aggregate
 8         HashSet<String> set = new HashSet<>();
 9         //Add Elements
10         set.add("Dema");
11         set.add("Debang");
12         set.add("Queen");
13         set.add("Blademaster");
14         set.add("Dema");
15         System.out.println(set);//[Debang, Queen, Dema, Blademaster]//Element does not allow duplication,And out of order
16         //Traverse Set
17         for (String s : set) {
18             System.out.println(s);
19         }
20     }
21 }

HashSet determines where elements are stored in the collection based on the object's hash value, so it has good access and lookup performance.The way elements are guaranteed to be unique depends on the hashCode and equals methods

HashSet collection structure for storing data (hash table)

Hash tables are implemented by Array + Chain List + Red-Black Tree (JDK1.8 adds the Red-Black Tree section), as shown in the following figure

  

  

HashSet stores custom type elements

When storing custom type elements in HashSet, you need to override hashCode and equals methods in the object to establish your own way of comparison to ensure that the objects in the HashSet collection are unique.

 1 package demosummary.set;
 2 
 3 import java.util.Objects;
 4 
 5 public class SetPerson {
 6     private String name;
 7     private int age;
 8 
 9     public SetPerson() {
10     }
11 
12     public SetPerson(String name, int age) {
13         this.name = name;
14         this.age = age;
15     }
16 
17     public String getName() {
18         return name;
19     }
20 
21     public void setName(String name) {
22         this.name = name;
23     }
24 
25     public int getAge() {
26         return age;
27     }
28 
29     public void setAge(int age) {
30         this.age = age;
31     }
32 
33     @Override
34     public boolean equals(Object o) {
35         if (this == o) return true;
36         if (o == null || getClass() != o.getClass()) return false;
37         SetPerson setPerson = (SetPerson) o;
38         return age == setPerson.age &&
39                 Objects.equals(name, setPerson.name);
40     }
41 
42     @Override
43     public int hashCode() {
44         return Objects.hash(name, age);
45     }
46 
47     @Override
48     public String toString() {
49         return "SetPerson{" +
50                 "name='" + name + '\'' +
51                 ", age=" + age +
52                 '}';
53     }
54 }
 1 package demosummary.set;
 2 
 3 import java.util.HashSet;
 4 
 5 public class SetPersonTest {
 6     public static void main(String[] args) {
 7         //Establish set aggregate
 8         HashSet<SetPerson> sp = new HashSet<>();
 9         //Add Elements
10         SetPerson setPerson = new SetPerson("Dema",18);
11         sp.add(setPerson);
12         sp.add(new SetPerson("Debang", 19));
13         sp.add(new SetPerson("Queen", 20));
14         sp.add(new SetPerson("Blademaster", 19));
15         //ergodic set aggregate
16         for (SetPerson person : sp) {
17             System.out.println(person);
18         }
19         /**
20          * results of enforcement
21          * SetPerson{name='Queen', age=20}
22          * SetPerson{name='Dema', age=18}
23          * SetPerson{name='Debon', age=19}
24          * SetPerson{name='Swordsman', age=19}
25          */
26     }
27 }

LinkedHashSet

A subclass of HashSet, java.util.LinkedHashSet, is a data storage structure that combines a chain table and a hash table to ensure orderly removal of elements

 1 package demosummary.set;
 2 
 3         import java.util.Iterator;
 4         import java.util.LinkedHashSet;
 5         import java.util.LinkedList;
 6 
 7 public class LinkedHashSetTest01 {
 8     public static void main(String[] args) {
 9         //Establish LinkedList aggregate
10         LinkedHashSet<String> set = new LinkedHashSet<>();
11         //Add Elements
12         set.add("Blademaster");
13         set.add("Dema");
14         set.add("Queen");
15         set.add("Debang");
16         //Iterator Output
17         Iterator<String> iterator = set.iterator();
18         while (iterator.hasNext()) {
19             System.out.println(iterator.next());
20         }
21         /**
22          * Output Results
23          * Blademaster
24          * Dema
25          * Queen
26          * Debang
27          */
28     }
29 }

Variable parameters

If we define a method that accepts multiple parameters and has the same type of parameters, we can simplify it to the following format:

Modifier Return Value Type Method Name (Parameter Type... Parameter Name) {}

- Equivalent to

Modifier return value type method name (parameter type [] parameter name) {}

 1 package demosummary.set;
 2 
 3 public class SetTest {
 4     public static void main(String[] args) {
 5         //Define an array
 6         int[] arr = {1,5,8,66,88,345};
 7         //Define a result and assign values
 8         int sum = getSum(arr);//Call the method and give sum assignment
 9         //Output Results
10         System.out.println(sum);
11     }
12 
13     /**
14      * Complete Array; Sum All Elements; Original Writing
15      * public static int getSum(int[] arr){
16      *      int sum = 0;
17      *      for(int a : arr){
18      *          sum += a;
19      *      }
20      *      return sum;
21      * }
22      */
23     //Variable Parameter Writing
24     public static int getSum(int[] arr){
25         //Define a sum parameter
26         int sum = 0;
27         //Enhance for
28         for (int i : arr) {
29             //Result
30             sum += i;
31         }
32         //Return results
33         return sum;
34     }
35 }

Tags: Java

Posted on Thu, 07 Nov 2019 11:13:06 -0800 by Niel Roos