HashSet保证元素唯一性源码展示
来源:互联网 发布:淘宝10元包邮专区 编辑:程序博客网 时间:2024/04/20 14:03
package CollectionTest;
/* HashSet保证元素唯一性源码展示
*
* interface Collection {
...
}
interface Set extends Collection {
...
}
class HashSet implements Set {
private static final Object PRESENT = new Object();
private transient HashMap<E,Object> map;
public HashSet() {
map = new HashMap<>();
}
public boolean add(E e) { //e=hello,world
return map.put(e, PRESENT)==null;
}
}
class HashMap implements Map {
public V put(K key, V value) { //key=e=hello,world
//看哈希表是否为空,如果空,就开辟空间
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
//判断对象是否为null
if (key == null)
return putForNullKey(value);
int hash = hash(key); //和对象的hashCode()方法相关
//在哈希表中查找hash值
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
//这次的e其实是第一次的world
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
//走这里其实是没有添加元素
}
}
modCount++;
addEntry(hash, key, value, i); //把元素添加
return null;
}
transient int hashSeed = 0;
final int hash(Object k) { //k=key=e=hello,
int h = hashSeed;
if (0 != h && k instanceof String) {
return sun.misc.Hashing.stringHash32((String) k);
}
h ^= k.hashCode(); //这里调用的是对象的hashCode()方法
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
}
*/
import java.util.HashSet;
import java.util.Set;
public class HashSetDemo {
public static void main(String[] args) {
Set hs=new HashSet();
hs.add("hello");
hs.add("world");
}
}
/* HashSet保证元素唯一性源码展示
*
* interface Collection {
...
}
interface Set extends Collection {
...
}
class HashSet implements Set {
private static final Object PRESENT = new Object();
private transient HashMap<E,Object> map;
public HashSet() {
map = new HashMap<>();
}
public boolean add(E e) { //e=hello,world
return map.put(e, PRESENT)==null;
}
}
class HashMap implements Map {
public V put(K key, V value) { //key=e=hello,world
//看哈希表是否为空,如果空,就开辟空间
if (table == EMPTY_TABLE) {
inflateTable(threshold);
}
//判断对象是否为null
if (key == null)
return putForNullKey(value);
int hash = hash(key); //和对象的hashCode()方法相关
//在哈希表中查找hash值
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
//这次的e其实是第一次的world
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
//走这里其实是没有添加元素
}
}
modCount++;
addEntry(hash, key, value, i); //把元素添加
return null;
}
transient int hashSeed = 0;
final int hash(Object k) { //k=key=e=hello,
int h = hashSeed;
if (0 != h && k instanceof String) {
return sun.misc.Hashing.stringHash32((String) k);
}
h ^= k.hashCode(); //这里调用的是对象的hashCode()方法
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
}
*/
import java.util.HashSet;
import java.util.Set;
public class HashSetDemo {
public static void main(String[] args) {
Set hs=new HashSet();
hs.add("hello");
hs.add("world");
}
}
0 0
- HashSet保证元素唯一性源码展示
- HashSet保证元素唯一性
- HashSet如何保证元素的唯一性-跟踪源码解析
- HashSet保证元素唯一性原理图解
- HashSet保证元素唯一性的原理
- HashSet如何保证元素唯一性?
- HashSet如何保证元素的唯一性
- 分析HashSet,HashMap源码,如何保证HashSet集合元素唯一性。
- 关于HashSet集合是如何保证元素唯一性的
- HashSet集合是如何保证元素的唯一性的?
- HashSet集合是如何保证元素的唯一性的?
- java--基础----HashSet保证元素的唯一性
- 集合框架-HashSet存储元素保证唯一性的图解
- HashSet存储元素保证唯一性的代码及图解
- 集合框架_HashSet保证元素唯一性的源码解析
- java学习笔记之 HashSet 保证元素唯一性的方法
- 《java入门第一季》之HashSet存储元素保证唯一性的代码及图解
- Java_基础—HashSet存储自定义对象保证元素唯一性
- 51nod 编辑距离问题 dp
- The Apache Tomcat installation at this directory is version 8.5.6A Tomcat 8.0 installation is expec
- sizeof运算符
- HibernateTemplate、HibernateDaoSupport两种方法实现增删改查Good
- 内存映射文件详解-----C++实现
- HashSet保证元素唯一性源码展示
- MANIFEST.MF
- leetcode (23) - Merge k Sorted Lists
- 机器学习 小白笔记 正式开始!
- CheckBox设置颜色和自定义样式
- POJ3185 高斯消元 +枚举自由变元
- 18.海量分布式存储系统 Doris 的高可用架构设计分析
- 马尔科夫随机过程的理解
- opencv cascade训练LBP特征进行目标检测