简体   繁体   中英

How do HashSet and HashMap work in Java?

I'm a bit confused about the internal implementation of HashSet and HashMap in java.

This is my understanding, so please correct me if I'm wrong:

Neither HashSet or HashMap allow duplicate elements.

HashSet is backed by a HashMap , so in a HashSet when we call .add(element) , we are calling the hashCode() method on the element and internally doing a put(k,v) to the internal HashMap , where the key is the hashCode and the value is the actual object. So if we try to add the same object to the Set , it will see that the hashCode is already there, and then replace the old value by the new one.

But then, this seems inconsistent to me when I read how a HashMap works when storing our own objects as keys in a HashMap . In this case we must override the hashCode() and equals() methods and make them consistent between each other, because, if we find keys with the same hashCode , they will go to the same bucket, and then to distinguish between all the entries with the same hashCode we have to iterate over the list of entries to call the method equals() on each key and find a match. So in this case, we allow to have the same hashCode and we create a bucket containing a list for all the objects with the same hashCode , however using a HashSet , if we find already a hashCode , we replace the old value by the new value.

I'm a bit confused, could someone clarify this to me please?

You are correct regarding the behavior of HashMap , but you are wrong about the implementation of HashSet .

HashSet is backed by a HashMap internally, but the element you are adding to the HashSet is used as the key in the backing HashMap . For the value, a dummy value is used. Therefore the HashSet 's contains(element) simply calls the backing HashMap 's containsKey(element) .

The value we insert in HashMap acts as a Key to the map object and for its value, java uses a constant variable .So in the key-value pair , all the keys will have the same value .

you can refer to this link https://www.geeksforgeeks.org/hashset-in-java/

Hash Map:-Basically Hash map working as key and value ,if we want to store data as key and value pair then we will go to the hash map, basically when we insert data by using hash map basically internally it will follow 3 think, 1.hashcode 2..equale 3.==

when we insert the data in hash map it will store the data in bucket(fast in) by using hash code , if there is 2 data store in the same bocket then key collision will happen to resolve this key collision we use (==) method, always == method check the reference of the object, if both object hashcode is same then first one replace to second one if the hashcode is not same then hashing Collision will happen to resolve this hashing collision we will use (.equal) method .equal method basically it will check the content , if both the content is same then it will return true other wise it will return false, so in the hash map it will check is the content is same ? if the content is same then first one replace to the second one if both content is different the it will create another one object in the bocket and store the data

Hash Set:- Basically Hash Set is use to store bunch of object at a time ,internally hash set also use hash map only , when we insert somethink by using add method internally it will call put method and it will store data in the hashmap key bcz hash map key always unique and duplicate are not allowed that's way hashset also unique and duplicate are not allowed and if we entered duplicate also in hashst it will not through any exception first one will replace to the second one and in the value it will store constant data "PRESENT".

You can observe that internal hashmap object contains the element of hashset as keys and constant “PRESENT” as their value. Where present is constant which is defined as private static final Object present = new Object()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM