Java Road --- Day19 (set Interface)

set Interface

  java.util.Set java.util.List interfaces and interfaces, same Inherited from Collection Interface, which is consistent with the Collection interface method, but set unordered interface elements, and will not be repeated

  classification

    1.HashSet set

    2.LinkedHashSet collection

HashSet set:

  java.util.HashSet is an implementation of the Set interface, its stored element is not repeatable, and the elements are disordered (i.e., inconsistent access sequence)

. 1  Package demosummary.set;
 2  
. 3  Import java.util.HashSet with;
 . 4  
. 5  public  class HashSetTest01 {
 . 6      public  static  void main (String [] args) {
 . 7          // Create a collection set 
. 8          HashSet <String> = set new new HashSet <> ();
 9          // additive elements 
10          set.add ( "Del Mar" );
 . 11          set.add ( "Nadu" );
 12 is          set.add ( "Prince" );
 13 is          set.add ( "Juggernaut" ) ;
 14         set.add ( "Del Mar" );
 15          System.out.println (SET); // [Nadu, Prince, Del Mar, Juggernaut] // element must be unique, and disorderly
 16          // iterate set 
17          for (String S: SET) {
 18 is              System.out.println (S);
 . 19          }
 20 is      }
 21 is }

  HashSet is determined based on a hash value of the object storage locations within the collection, and it has good access and search performance. The only way to ensure the elements depends on: hashCode with equals

HashSet set of structures for storing data (hash table)

  Hash table is an array of red-black tree + + chain (JDK1.8 increased red-black tree part) implemented, as shown in FIG.

  

  

HashSet custom type storage element

  HashSet when stored in a custom type elements need to be rewritten object hashCode and equals methods, to establish their own way of comparison, in order to ensure the collection of unique objects HashSet 

 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         //创建set集合
 8         HashSet<SetPerson> sp = new HashSet<>();
 9         //添加元素
10         SetPerson setPerson = new SetPerson("德玛",18);
11         sp.add(setPerson);
12         sp.add(new SetPerson("德邦", 19));
13         sp.add(new SetPerson("皇子", 20));
14         sp.add(new SetPerson("剑圣", 19));
15         //遍历set集合
16         for (SetPerson person : sp) {
17             System.out.println(person);
18         }
19         /**
20          * 执行结果
21          * SetPerson{name='皇子', age=20}
22          * SetPerson{name='德玛', age=18}
23          * SetPerson{name='德邦', age=19}
24          * SetPerson{name='剑圣', age=19}
25          */
26     }
27 }

LinkedHashSet

  HashSet的一个子类 java.util.LinkedHashSet ,它是链表和哈希表组合的一个数据存储结构,保证取出元素是有序的

 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         //创建LinkedList集合
10         LinkedHashSet<String> set = new LinkedHashSet<>();
11         //添加元素
12         set.add("剑圣");
13         set.add("德玛");
14         set.add("皇子");
15         set.add("德邦");
16         //迭代器输出结果
17         Iterator<String> iterator = set.iterator();
18         while (iterator.hasNext()) {
19             System.out.println(iterator.next());
20         }
21         /**
22          * 输出结果
23          * 剑圣
24          * 德玛
25          * 皇子
26          * 德邦
27          */
28     }
29 }

可变参数

  如果我们定义一个方法需要接受多个参数,并且多个参数类型一致,我们可以对其简化成如下格 式:

  修饰符 返回值类型 方法名(参数类型... 形参名){  }

  等价于

  修饰符 返回值类型 方法名(参数类型[] 形参名){  }

 1 package demosummary.set;
 2 
 3 public class SetTest {
 4     public static void main(String[] args) {
 5         //定义一个数组
 6         int[] arr = {1,5,8,66,88,345};
 7         //定义一个结果并赋值
 8         int sum = getSum(arr);//调用方法并给sum赋值
 9         //输出结果
10         System.out.println(sum);
11     }
12 
13     /**
14      * 完成数组  所有元素的求和 原始写法
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     //可变参数写法
24     public static int getSum(int[] arr){
25         //定义一个sum参数
26         int sum = 0;
27         //增强for
28         for (int i : arr) {
29             //得出结果
30             sum += i;
31         }
32         //返回结果
33         return sum;
34     }
35 }

 

 

Guess you like

Origin www.cnblogs.com/hpcz190911/p/11817261.html