用HashSet的add方法谈hashcode和equals方法重写

本文主要通过用HashSet的add方法讲一下hashCode和equals方法重写。错误的地方望指正。

1.了解HashSet的add方法

了解一个方法的好办法是看源码,所以先看源码

private transient HashMap<E,Object> map;
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
public boolean add(E e) {
return map.put(e, PRESENT)==null;
} 

 由上面可以知道HashSet里面是用的HashMap处理,add方法其实是用了map的put方法

 1 transient Entry<K,V>[] table;
 2 transient int modCount;
 3 public V put(K key, V value) {
 4         if (key == null)
 5             return putForNullKey(value);                                             
 6         int hash = hash(key);                                                       
 7         int i = indexFor(hash, table.length);
 8         for (Entry<K,V> e = table[i]; e != null; e = e.next) {
 9             Object k;
10             if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
11                 V oldValue = e.value;
12                 e.value = value;
13                 e.recordAccess(this);
14                 return oldValue;
15             }
16         }
17         modCount++;
18         addEntry(hash, key, value, i);
19         return null;
20

    table即前面已经存在的数据,这里会将已存在的内容的key与当前key做比较,e.hash == hash && ((k = e.key) == key || key.equals(k)),其中hash是根据hashcode计算,一般情况下,hashCode一致,hash也是一样的。如果比较的是true的话,将已存在的value改成新的。对于hashset其实就原值没变只是在内部的hashmap的value重新放了new Object().

 2.例子

到这里我想应该都知道,如何来重写了。这里简单做一个例子。

public class UnlockGood {
    public UnlockGood(){}
    public UnlockGood(String skuNo, int wmsId,int count) {
        super();
        this.skuNo = skuNo;
        this.count = count;
        this.wmsId = wmsId;
    }
    //商品编码
    private String skuNo;
    //数量
    private int count;
    //仓库ID
    private int wmsId;
    public String getSkuNo() {
        return skuNo;
    }
    public void setSkuNo(String skuNo) {
        this.skuNo = skuNo;
    }
    public int getCount() {
        return count;
    }
    public void setCount(int count) {
        this.count = count;
    }
    public int getWmsId() {
        return wmsId;
    }
    public void setWmsId(int wmsId) {
        this.wmsId = wmsId;
    }
    @Override
    public boolean equals(Object obj) {
        UnlockGood good = (UnlockGood)obj;
        if(this==obj){
            return true;
        }else if(this.getSkuNo().equals(good.getSkuNo())&&this.getWmsId()==good.getWmsId()){
            good.setCount(good.getCount()+this.getCount());
            return true;
        }else{
            return false;
        }
    }
    @Override
    public int hashCode() {
        //随便写一个hashCode
        return 1;
    }
}

上面是重写的hashCode和equals方法,下面是main方法

        Set<UnlockGood> unlockgoods = new HashSet<UnlockGood>();
        UnlockGood good =  new UnlockGood("a",1,2);
        unlockgoods.add(good);
        UnlockGood good1 = new UnlockGood("a",1,12);
        unlockgoods.add(good1);
        UnlockGood good2 = new UnlockGood("b",1,2);
        unlockgoods.add(good2);

这里利用的是如果hash一致的时候,会调用equals方法,当然如果是同一个key就不会调用equals方法的。利用这个特性对原始值进行修改,达到自己想要的元素加入规则。

猜你喜欢

转载自www.linuxidc.com/Linux/2016-12/138083.htm
今日推荐