Use the add method of HashSet to overwrite hashcode and equals, and use hashsethashcode

Source: Internet
Author: User

Use the add method of HashSet to overwrite hashcode and equals, and use hashsethashcode

This article describes how to use the add method of HashSet to overwrite the hashCode and equals methods. Correct the error.

1. Understand the add method of HashSet

A good way to understand a method is to look at the source code, so first look at the source code

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

 

We can see from the above that HashMap is used for processing in the HashSet, And the add method actually uses the put Method of map.

 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 is the data that already exists. Here, the key of the existing content is compared with the current key, e. hash = hash & (k = e. key) = key | key. equals (k), where hash is calculated based on hashcode. Generally, the hashCode is consistent, and the hash is the same. If the value is true, change the existing value to a new one. In fact, the original value of hashset is not changed, but the value of the internal hashmap is re-stored with new Object ().

2. Example

Here I should know how to rewrite it. Here is a simple example.

Public class UnlockGood {public UnlockGood () {} public UnlockGood (String skuNo, int wmsId, int count) {super (); this. skuNo = skuNo; this. count = count; this. wmsId = wmsId;} // product code private String skuNo; // quantity private int count; // repository 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 () {// write a hashCode return 1 ;}}

The hashCode and equals methods are overwritten. The main method is used below.

        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);        

 

In this example, the equals method is called when the hash value is consistent. Of course, if the hash value is the same key, the equals method is not called. Use this feature to modify the original value to add the desired element to the rule.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.