The problem of the Add method of HashSet __hashset

Source: Internet
Author: User
The problem of the Add method of HashSet

Yesterday, the problem was knocked write a class of their own, and then use Set filter Repeat, met the Equals and hashcode problems, coupled with the interview before the relevant content, feel that they are still shallow, so with doubt have looked at the HashMap code, will be summed up a few questions. about HashSet incoming objects, how to filter

First of all, we know that all sets have a map in it, and using the key value of the map, value holds a fixed object, which is the adapter pattern .

So the question is, if you pass in two objects, what determines if they are equal, will the old replace the new object, and how do you replace or not replace it? is that equal?

if (P.hash = = Hash && (k = p.key) = = Key | | (Key!= null && key.equals (k)))

This is the code to judge the statement, you can see that here with the hash and equals are compared, it takes two to return true.
What needs to be explained is that the hash is not equal to the Hashcode method, but is calculated according to the hashcode, the result linear correlation.

Static final int hash (Object key) {
    int h;
    return (key = = null)? 0: (H = key.hashcode ()) ^ (h >>>);
}

If we do not implement Hashcode or equals, both will invoke the method on object, the Hashcode method on object is a local method, the value returned should be the object in the memory address, and the Equals method is determined by = = To determine whether the address is consistent.

The methods under Object and some comments doc
/*as much as are reasonably practical, the Hashcode method defined by
* class {@code Object} Does return distinct integers for distinct
* objects. (This is typically implemented by converting the internal
* address of the object in an integer, but this Implementa tion
* Technique is isn't required by the
* java™ programming language.)
* * * The
comment above means that the object's Hashcode method requires that different ints return different int
* * The most common implementation is to return the internal address of the object converted to int
* But in the Java language does not specify the implementation of technical details * * * public
native int hashcode ();
public boolean equals (Object obj) {return
    (this = = obj);
}

So, the conclusion of this question is that the hashcode and the Equals method are used to judge the equality, the hash equivalent based on the hashcode (which is somewhat different from the direct requirement hashcode), and the Equals returns True when the two are judged to be equal. will the old ones replace the new objects?

Value we know that the old will be replaced by the new incoming value, then key?
The answer is not to replace, is still the old value, in fact, this conclusion try to come out, so next, we say the details, that is how to judge not to replace? How do I judge not to replace?

We mentioned earlier that the Map,set operation in set is appropriate to the map, so the Set.add method is also the case, and it should be noted that the Add method has a return value if the duplicate value that exists in the incoming set returns FASLE to indicate that the add failed. Passing in a new value that does not exist returns true to indicate that add succeeded.

Public boolean Add (E. e) {return
    map.put (E, PRESENT) ==null;
}

This determines whether the operation of Map.put is null to return TRUE or false, and map.put what happens when NULL is returned and returns NULL. Hashmap.put Implementation Details

Hashmap.put is actually handing the put operation to another method Putval

Public V-Put (K key, v. value) {return
    Putval (hash (key), key, value, false, True);
}

The Putval method signature is as follows:

Final V putval (int hash, K key, V value, Boolean Onlyifabsent,boolean evict)

This method returns a V, which is the value in the map, and looks at the code to see:
1. If put into the map is an existing key, then the key corresponding to the value of the update, will return the old value, that is, oldvalue
2. If the new key is put into the map, then NULL is returned

This way, you can string up:
1. Hashmap.put incoming new Key-value, return null,hashset The result is null, the Add method returns true to increase the success of
2. Hashmap.put Pass in existing key and new value, return Oldvalue,hashset The result is not NULL, the Add method returns false indicating an increase failure

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.