1. 程式人生 > 其它 >Collection介面之子介面Set

Collection介面之子介面Set

Set介面的實現類:hashSet,linkedHashSet,TreeSet;

Set介面使用的比較少,瞭解即可

  1 /**
  2  * Collection介面之Set子介面
  3  * set介面:無序的不可重複的資料
  4  * HashSet:作為set介面的主要實現類,執行緒不安全效率高,可以儲存null值
  5  *        LinkedHashSet:作為HashSet的子類,遍歷內部資料時,可以按照儲存的順序遍歷
  6  * TreeSet:可以按照新增元素的指定屬性進行排序
  7  * ********************
  8  * 要求:向Set中新增資料一定要重寫其資料所在類的equals與hashCode方法
9 ********************** 10 * 無序性:不代表隨機性,儲存的資料在底層陣列中並非按照索引的順序排列,是按照hashcode值進行排序 11 * 不可重複性:保證新增的元素按照equals方法判斷時,不能返回true,即相同的元素只可以有一個 12 *二:新增元素的過程:以hashset為例: 13 * 我們向hashset中新增元素a,首先呼叫a所在類的hashCode方法,計算出a的雜湊值 14 * 此雜湊值再通過某種演算法計算出再HashSet底層陣列中存放的位置,判斷陣列此位置上是否已經有元素 15 * 如果此位置無元素,則a新增成功,如果有元素b,則比較a與b的雜湊值,如果雜湊值不相同則a新增成功
16 * 如果雜湊值相同則需要呼叫元素a所在類的equals方法,如果方法返回true,a新增失敗,否則新增成功 17 * 18 * 19 * 20 */ 21 public class SetTest { 22 23 @Test 24 public void test() { 25 HashSet set=new HashSet(); 26 set.add(123); 27 set.add(456); 28 set.add("hpp"); 29 set.add("hpp"); 30 set.add(LocalDateTime.now());
31 System.out.println(set); 32 } 33 @Test 34 public void test2(){ 35 Set set=new LinkedHashSet(); 36 set.add(123); 37 set.add("hpp"); 38 set.add(426); 39 System.out.println(set); 40 } 41 @Test 42 public void test3(){ 43 /** 44 * treeSet不能新增不同類的物件:只能是相同類的物件 45 * 兩種排序方式:自然排序與定製排序,用的Comparable介面與Comparator介面 46 * 47 */ 48 TreeSet set=new TreeSet(); 49 //set.add("hpp"); 50 set.add(new Student(5,"zh")); 51 set.add(new Student(15,"si")); 52 set.add(new Student(25,"huang")); 53 set.add(new Student(6,"tang")); 54 set.add(new Student(6,"li")); 55 System.out.println(set); 56 for (Object o:set){ 57 System.out.println(o); 58 } 59 60 } 61 @Test 62 public void test4(){ 63 //按照年齡從大到小排序,年齡相同按照姓名排序 64 Comparator comparator=new Comparator() { 65 @Override 66 public int compare(Object o1, Object o2) { 67 if (o1 instanceof Student&& o2 instanceof Student) { 68 Student student1 = (Student) o1; 69 Student student2 = (Student) o2; 70 int a = Integer.compare(student1.getAge(), student2.getAge()); 71 if (a == 0) { 72 return student1.getName().compareTo(student2.getName()); 73 } else { 74 return Integer.compare(student1.getAge(), student2.getAge()); 75 } 76 } 77 throw new RuntimeException("型別錯誤"); 78 } 79 }; 80 TreeSet set=new TreeSet(comparator);//後面如果為空參代表使用自然排序,使用形參為定製排序 81 set.add(new Student(5,"zh")); 82 set.add(new Student(15,"si")); 83 set.add(new Student(25,"huang")); 84 set.add(new Student(6,"tang")); 85 set.add(new Student(6,"li")); 86 System.out.println(set); 87 for (Object o:set){ 88 System.out.println(o); 89 } 90 } 91 92 93 } 94 class Student implements Comparable{ 95 private int age; 96 private String name; 97 98 @Override 99 public String toString() { 100 return this.age+this.name; 101 } 102 103 public int getAge() { 104 return age; 105 } 106 107 public void setAge(int age) { 108 this.age = age; 109 } 110 111 public String getName() { 112 return name; 113 } 114 115 public void setName(String name) { 116 this.name = name; 117 } 118 119 public Student(int age, String name) { 120 this.age = age; 121 this.name = name; 122 } 123 //按照姓名從小到大排列 124 // @Override 125 // public int compareTo(Object o) { 126 // if(o instanceof Student){ 127 // Student student=(Student)o; 128 // return -this.getName().compareTo(student.getName()); 129 // }else { 130 // throw new RuntimeException("型別不匹配"); 131 // } 132 // } 133 /** 134 * 135 ********************************** 136 */ 137 //按照年齡排序:必須對於基本資料型別必須判斷大小,返回一個整數值 138 @Override 139 public int compareTo(Object o) { 140 if(o instanceof Student){ 141 Student student=(Student)o; 142 if (this.age>student.age){ 143 return 1; 144 }else if (this.age<student.age){ 145 return -1; 146 }else { 147 return -this.name.compareTo(student.name); 148 } 149 } 150 throw new RuntimeException("資料型別異常"); 151 } 152 }