java 集合子类_Java 集合-Set接口及其子类

2017-10-31 19:20:45

Set:无序且唯一

实现子类:HashSet,

HashSet

此类实现 Set 接口,由哈希表(实际上是一个 HashMap 实例)支持。它不保证 set 的迭代顺序;特别是它不保证该顺序恒久不变。此类允许使用 null 元素。

注意,此实现不是同步的。如果多个线程同时访问一个哈希 set,而其中至少一个线程修改了该 set,那么它必须 保持外部同步。

HashSet底层数据结构是哈希表(HashMap),哈希表依赖于哈希值存储,添加功能底层依赖两个方法:int hashCode(),boolean equals(Object obj)。

*构造方法

20180111004641269997.png

*常用方法

20180111004641271951.png

HashSet唯一性的解释:

源码剖析:添加功能底层依赖两个方法:int hashCode(),boolean equals(Object obj)

//HashSet类

class HashSet implements Set{

private static final Object PRESENT = new Object();

private transient HashMap map;

// 构造方法,返回了一个HashMap

public HashSet() {

map = new HashMap<>();

}

//add方法

public boolean add(E e) {

return map.put(e, PRESENT)==null;

}

}

//HashMap类

class HashMap implements Map{

public V put(K key, V value) {

return putVal(hash(key), key, value, false, true);

}

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,

boolean evict) {

Node[] tab; Node p; int n, i;

if ((tab = table) == null || (n = tab.length) == 0)

n = (tab = resize()).length;

if ((p = tab[i = (n - 1) & hash]) == null)

tab[i] = newNode(hash, key, value, null);

else {

Node e; K k;

if (p.hash == hash &&

((k = p.key) == key || (key != null && key.equals(k))))

e = p;

else if (p instanceof TreeNode)

e = ((TreeNode)p).putTreeVal(this, tab, hash, key, value);

else {

for (int binCount = 0; ; ++binCount) {

if ((e = p.next) == null) {

p.next = newNode(hash, key, value, null);

if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st

treeifyBin(tab, hash);

break;

}

if (e.hash == hash &&

((k = e.key) == key || (key != null && key.equals(k))))

break;

p = e;

}

}

if (e != null) { // existing mapping for key

V oldValue = e.value;

if (!onlyIfAbsent || oldValue == null)

e.value = value;

afterNodeAccess(e);

return oldValue;

}

}

++modCount;

if (++size > threshold)

resize();

afterNodeInsertion(evict);

return null;

}

}

一个实例问题,在这种添加自定义对象的时候,两个类的属性值相等,但是依然会被判定为不同的元素,因为没有重写hashCode(),所以默认调用的是Object类的hashCode(),而不同类的hashCode一般是不同的。

HashSet hashSet = new HashSet<>();

Student s1 = new Student("刘亦菲", 22);

Student s2 = new Student("章子怡", 25);

Student s3 = new Student("刘亦菲", 22);

hashSet.add(s1);

hashSet.add(s2);

hashSet.add(s3);

System.out.println(hashSet);

解决方法就是自己重写hashCode() 和 equals()方法:

public class Student {

private String name;

private Integer age;

Student(String name,int age)

{

this.name=name;

this.age=age;

}

@Override

public int hashCode() {

// return 0;

return this.name.hashCode()+this.age*11;

}

@Override

public boolean equals(Object obj) {

if(this == obj)

return true;

if(!(obj instanceof Student))

return false;

Student s = (Student) obj;

return this.name.equals(s.name) && this.age.equals(s.age);

}

}

这里用到了instanceof操作符,这个操作符和== ,>=是同种性质的,只是是用英文描述的,是二元操作符,用来判断左边的是否为这个特定类或者是它的子类的一个实例。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值