1. 程式人生 > >java集合之set

java集合之set

ash 字符數組 his err new rgs return 清除 single

  1 public class Demo1_Set {
  2 
  3     /*
  4      * set集合無序、不可重復、無索引
  5      */
  6     public static void main(String[] args) {
  7 
  8         //demo1();
  9         HashSet<Student> hs = new HashSet<>();
 10         hs.add(new Student("張三",19));
 11         hs.add(new Student("張三",19));
12 hs.add(new Student("李四",20)); 13 hs.add(new Student("李四",20)); 14 hs.add(new Student("李四",20)); 15 System.out.println(hs); 16 /* 17 註意: 向集合中添加自定義類對象時,要想不添加重復的數據(如:同姓名、同年齡認為是同一個人),需要在自定義類中重寫equals和hashCode方法 18 執行結果如下: 19 -----------------------
20 執行了嗎 21 執行了嗎 22 執行了嗎 23 執行了嗎 24 [Student [name=李四, age=20], Student [name=張三, age=19]] 25 -------------------------------- 26 @Override 27 public boolean equals(Object arg0) { 28 System.out.println("執行了嗎");
29 Student s = (Student) arg0; 30 return this.name.equals(s.name) && this.age == s.age; 31 } 32 33 @Override 34 public int hashCode() { 35 return 1; 36 } 37 */ 38 } 39 40 public static void demo1() { 41 HashSet<String> hs = new HashSet<>(); 42 boolean b1 = hs.add("a"); 43 boolean b2 = hs.add("a"); 44 boolean b3 = hs.add("c"); 45 boolean b4 = hs.add("d"); 46 boolean b5 = hs.add("e"); 47 boolean b6 = hs.add("f"); 48 49 System.out.println(b1); // true 50 System.out.println(b2); //false 51 System.out.println(hs); //[f, d, e, c, a] 52 for (String string : hs) { 53 System.out.print(string); //fdeca Set集合類有實現toString的方法,所以打印對象時是打印的值 54 } 55 } 56 57 } 58 59 -------------------------------------------------------------------------------- 60 61 public class Student implements Comparable<Student> { 62 private String name; 63 private int age; 64 65 @Override 66 public String toString() { 67 return "Student [name=" + name + ", age=" + age + "]"; 68 } 69 /* 70 * @Override public boolean equals(Object arg0) { System.out.println("執行了嗎"); 71 * Student s = (Student) arg0; return this.name.equals(s.name) && this.age == 72 * s.age; } 73 * 74 * 75 * @Override public int hashCode() { return 1; } 76 */ 77 78 public Student(String name, int age) { 79 super(); 80 this.name = name; 81 this.age = age; 82 } 83 84 @Override 85 public int hashCode() { 86 final int prime = 31; 87 int result = 1; 88 result = prime * result + age; 89 result = prime * result + ((name == null) ? 0 : name.hashCode()); 90 return result; 91 } 92 93 @Override 94 public boolean equals(Object obj) { 95 if (this == obj) 96 return true; 97 if (obj == null) 98 return false; 99 if (getClass() != obj.getClass()) 100 return false; 101 Student other = (Student) obj; 102 if (age != other.age) 103 return false; 104 if (name == null) { 105 if (other.name != null) 106 return false; 107 } else if (!name.equals(other.name)) 108 return false; 109 return true; 110 } 111 112 public String getName() { 113 return name; 114 } 115 116 public void setName(String name) { 117 this.name = name; 118 } 119 120 public int getAge() { 121 return age; 122 } 123 124 public void setAge(int age) { 125 this.age = age; 126 } 127 128 /* 129 * 優先比較姓名的長度,其次比較姓名的內容,再比較年齡 130 */ 131 @Override 132 public int compareTo(Student arg0) { 133 int length = this.name.length() - arg0.name.length(); 134 int num = length == 0 ? this.name.compareTo(arg0.name) : length; 135 return num == 0 ? this.age - arg0.age : num; 136 } 137 138 /* 139 * 優先按照姓名排序,之後再按照年齡排序 140 * 141 * @Override public int compareTo(Student arg0) { int num = 142 * this.name.compareTo(arg0.name); return num == 0 ? this.age - arg0.age : num; 143 * } 144 */ 145 146 /* 147 * 優先按照年齡排序,之後再按照姓名排序 148 * 149 * @Override public int compareTo(Student arg0) { int num = this.age - arg0.age; 150 * return num == 0 ? this.name.compareTo(arg0.name) : num; } 151 */ 152 153 } 154 155 ------------------------------------------------------------------------ 156 157 public class Demo2_Quchong { 158 159 /* 160 * 需求:將一個集合中的重復元素去除掉 161 * 分析: 162 * 1.獲取一個擁有重復元素的list集合 163 * 2.將list集合中的元素添加到set集合中 164 * 3.將list集合中的元素清除 165 * 4.將獲取到的set集合添加到清空了的list集合中 166 * 167 * LinkedHashSet 集合怎麽存進去的就怎麽取出來 168 * TreeSet 集合是可以對存進去的集合進行排序,同樣也可以保證集合中元素的唯一行 169 */ 170 public static void main(String[] args) { 171 //* 1.獲取一個擁有重復元素的list集合 172 ArrayList<String> list = new ArrayList<>(); 173 list.add("a"); 174 list.add("a"); 175 list.add("b"); 176 list.add("b"); 177 list.add("b"); 178 list.add("c"); 179 list.add("c"); 180 list.add("c"); 181 list.add("c"); 182 183 //通過方法去除重復元素 184 getSingle(list); 185 186 //打印list集合 187 System.out.println(list); 188 189 } 190 191 public static void getSingle(List<String> list) { 192 193 //2.將list集合中的元素添加到set集合中 194 LinkedHashSet<String> lhs = new LinkedHashSet<>(); 195 lhs.addAll(list); 196 197 //3.將list集合中的元素清除 198 list.clear(); 199 200 //4.將獲取到的set集合添加到清空了的list集合中 201 list.addAll(lhs); 202 } 203 204 } 205 206 ------------------------------------------------------------------ 207 208 public class Demo3_Quchong { 209 210 /* 211 * 需求:通過鍵盤輸入一串字符,然後去除重復,輸出不同的字符 212 * 分析: 213 * 1.提示輸入一串字符 214 * 2.將這一串字符轉換成字符數組 215 * 3.將字符數組存入set集合 216 * 4.打印最終的結果 217 */ 218 public static void main(String[] args) { 219 //1.提示輸入一串字符 220 Scanner sc = new Scanner(System.in); 221 System.out.println("請輸入一串字符:"); 222 223 //2.將這一串字符轉換成字符數組 224 String line = sc.nextLine(); 225 char[] ch = line.toCharArray(); 226 227 //3.將字符數組存入set集合 228 HashSet<Character> hs = new HashSet<>(); 229 for (Character character : ch) { 230 hs.add(character); 231 } 232 233 //4.打印最終的結果 234 System.out.println(hs); 235 236 } 237 238 } 239 240 ------------------------------------------------------------------- 241 242 public class Demo4_TreeSet { 243 244 /* 245 * TreeSet 集合自動對集合進行排序,同樣也能滿足集合中元素的唯一性 246 * 247 * 當自定義對象沒有實現Comparable方法的時候,新增對象的時候會報類型轉換異常 248 * 當compareTo方法返回值為 0 的時候,集合中只會有一個元素被存進去 249 * 當compareTo方法返回值為正數的時候,怎麽存就怎麽取 250 * 當compareTo方法返回值為負數的時候,取的時候會倒序去值 251 */ 252 public static void main(String[] args) { 253 //demo1(); 254 //demo2(); 255 //demo3(); 256 TreeSet<String> ts = new TreeSet<>(new CompareByLen()); 257 ts.add("aaaaaaaa"); 258 ts.add("b"); 259 ts.add("cc"); 260 ts.add("avb"); 261 ts.add("nba"); 262 System.out.println(ts); 263 } 264 265 public static void demo3() { 266 TreeSet<Student> ts = new TreeSet<>(); 267 ts.add(new Student("zhangsan", 23)); 268 ts.add(new Student("lisi", 33)); 269 ts.add(new Student("zhaoliu", 43)); 270 ts.add(new Student("wangwu", 13)); 271 ts.add(new Student("aaaa", 13)); 272 System.out.println(ts); 273 } 274 275 public static void demo2() { 276 TreeSet<Student> ts = new TreeSet<>(); 277 ts.add(new Student("張三",23)); 278 ts.add(new Student("王五",22)); 279 ts.add(new Student("趙五",22)); 280 ts.add(new Student("李四",25)); 281 ts.add(new Student("趙六",20)); 282 System.out.println(ts); 283 } 284 285 public static void demo1() { 286 TreeSet<String> ts = new TreeSet<>(); 287 ts.add("c"); 288 ts.add("a"); 289 ts.add("b"); 290 ts.add("a"); 291 ts.add("b"); 292 ts.add("b"); 293 ts.add("a"); 294 295 System.out.println(ts); //[a, b, c] 296 } 297 298 } 299 300 class CompareByLen implements Comparator<String> { 301 302 @Override 303 public int compare(String s1, String s2) { 304 int num = s1.length() - s2.length(); 305 return num == 0 ? s1.compareTo(s2) : num; 306 } 307 308 }

java集合之set