java - What happens if we override only hashCode() in a class and use it in a Set? -
This can not be a real world scenario, but curious to know what happens below, code is.
I am creating a set of square objects As I have not overridden, the method is equal to, the default implementation will return false. So where will the object be placed with "one" in the same bucket, where the previous object is placed with the headshot 97? Or will it create a new bucket? Does anyone know how it will be stored internally? The output is: If two objects are identical according to the equality () method, they should have the same hash code. If two objects have the same hash code, they do not even have to be equal. Here is the actual paradigm to the java.util.Object document: In addition, it is also wrong to say that the hash code function is an efficient way to do anything, it is all dependent on the implementation, but the default implementation for the string hashode is very disabled because the string gets bigger It will calculate based on each character of the string, so if you are using a large string as the key, then it is very disabled; If you have a large number of buckets. In a map (HashSet internally uses a Hashmap), there are buckets and each bucket has a linked list. Java uses the hash code () function to detect it, it detects which bucket it is in (it will actually modify hash, how many buckets are present). Since two objects can share the same hash, it will be sequentially again through the linked list, given that the object is a duplicate, Java.util.Set documenation copy: A collection that has no duplicate element. Therefore, if its hashode () goes to a bucket, in which the bucket has an object where the .equals () is true, then the previous object overwrites the new object is done. You can probably look for more information here: However, it is a good practice, however, that if you overwrite the hashcode function, you also overwrite the equivalent function (if I am wrong If not, this contract breaks out if you do not choose). UsingSet according to the hashing concept in Java, when I have added the object in the first place that has "a" 97 will make a bucket and put the object in it. After experiencing an object with "A" again, it calls the override hashode method in class tools and it gets hashcode 97, so what's next?
/ * Whatever the package; // package does not keep the name! * / Import java.util *; Import java.lang *; Import java.io * *; Using Class {String value; Public UsingSet (string value) {this.value = value; } Public string toasting () {return value; } Public Action Hashod () {int hash = value.hashCode (); System.out.println ("hash code called" + hash); Return hash; } Public static zero principal (string algos []) {java.util.Set s = new java.util.HashSet (); S.add (new experiment ("A");) S.add (new experiment ("B");) S.add (new experiment ("a");) S.add (new experiment ("B");) S.add (new experiment ("a");) S.add (new integer (1)); S.add (new integer (1)); System.out.println ("s =" + s); }}
The hash code called is called the hasodode 98 hashode has been called the codehodd 9 898 hashode called 97 s = [1, b The big answer is wrong, or misleading (as well as the wrong part)
Comments
Post a Comment