s-day02-哈希表以及HashSet中去重复的原理(代码)
This commit is contained in:
@@ -0,0 +1,36 @@
|
|||||||
|
package com.inmind.set02;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
/*
|
||||||
|
哈希表以及HashSet中去重复的原理
|
||||||
|
|
||||||
|
Set集合底层的数据结构是哈希表
|
||||||
|
|
||||||
|
哈希表:在JDK8之前,就是数组+链表
|
||||||
|
哈希表:在JDK8之后,就是数组+链表+红黑树
|
||||||
|
*/
|
||||||
|
public class HashTableDemo09 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String str1 = "abc";
|
||||||
|
String str2 = new String("abc");
|
||||||
|
String str3 = "通话";
|
||||||
|
String str4 = "重地";
|
||||||
|
//查看他们各自的哈希值
|
||||||
|
System.out.println(str1.hashCode());
|
||||||
|
System.out.println(str2.hashCode());
|
||||||
|
System.out.println(str3.hashCode());
|
||||||
|
System.out.println(str4.hashCode());
|
||||||
|
|
||||||
|
//存入set集合
|
||||||
|
HashSet<String> sets = new HashSet<>();
|
||||||
|
sets.add(str1);
|
||||||
|
sets.add(str2);
|
||||||
|
//更换了通话和重地的添加顺序后,改变了sets集合的内容展示顺序,为什么呢??哈希表数据结构导致的
|
||||||
|
sets.add(str4);
|
||||||
|
sets.add(str3);
|
||||||
|
System.out.println(sets);
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user