Java Collection HashSet vs HashMap [duplicate] - java

This question already has answers here:
Difference between HashSet and HashMap?
(20 answers)
Closed 8 years ago.
I have Question regarding the best practice of using a Collection regarding the memory. I need to call a method which returns pairs of (Key,Value) frequently, so which way is the best, using HashMap or creating an Object that contains Key and value and save this object in a HashSet?
Thanks & Regards.

It depends on whether you need to search the data structure based on the key alone or both the key and the value.
If you search by the key alone (i.e. map.containsKey(key)), you should use a HashMap.
If you search for existence of a key-value pair (i.e. set.contains(new Pair(key,value)), you should use a HashSet that contains those pairs.
Another thing to consider is how you determine the uniqueness of the elements. If it is determined by the key alone, you should use a HashMap. If it is determined by both key and value (i.e. you can have the same key appear twice with different values), you must use HashSet, since HashhMap doesn't allow the same key to appear more than once.

Related

Hash Map allow null insertion but Hash table won't allow null insertion? [duplicate]

This question already has answers here:
Why Hashtable does not allow null keys or values?
(10 answers)
Closed 3 years ago.
We can do null insertion in Hash Map for both Key and value but in case of Hash table we are not able to
do null insertion?
To successfully store and retrieve objects from a HashTable, the objects used as keys must implement the hashCode method and the equals method. Since null is not an object, it can’t implement these methods. HashMap is an advanced version and improvement on the Hashtable. HashMap was created later.
Null key in hashmap is always stored on 1st position. so when ever you request a null key in hashmap its go for first position. retrive the key and value. than return it.
Only 1 null key is allowed in hashmap as it can not contain duplicate keys.
ConcurrentHashMap is a newer class but also has the restriction of not allowing null keys or values. They add this restriction for performance reasons since it's a lot of extra work to support null keys and values but probably not useful the majority of the time.

What is the role played by hashCode() in a HashSet or HashMap [duplicate]

This question already has answers here:
How does a hash table work?
(17 answers)
Closed 6 years ago.
The top voted answer of this question states:
When using a hash-based Collection or Map such as HashSet, LinkedHashSet, HashMap, Hashtable, or WeakHashMap, make sure that the hashCode() of the key objects that you put into the collection never changes while the object is in the collection. The bulletproof way to ensure this is to make your keys immutable, which has also other benefits.
Honestly I don't understand what is the role played by hashCode() with regards to HashSet or HashMap collection objects.
How hashCode() method of key objects is being used in hash-based Collections?
Which could be the case where hashCode() of objects change in their lifetime?
Can someone elaborate furtherly about this aspect with some example?
As very clearly stated on this blog post, discussing: How HashMap works in Java
HashMap in Java works on hashing principle. It is a data structure which allows us to store object and retrieve it in constant time O(1) provided we know the key. In hashing, hash functions are used to link key and value in HashMap. Objects are stored by calling put(key, value) method of HashMap and retrieved by calling get(key) method. When we call put method, hashcode() method of the key object is called so that hash function of the map can find a bucket location to store value object, which is actually an index of the internal array, known as the table.
Read more: http://javarevisited.blogspot.com/2011/02/how-hashmap-works-in-java.html#ixzz42bvBzm6Y

JAVA: HashCode : Is it possible more than one elemets has same hashcode? [duplicate]

This question already has answers here:
How do HashTables deal with collisions?
(10 answers)
Closed 8 years ago.
I have read in a book that
"The value received from hashcode() is used as bucket number for storing elements."
My doubt is if the bucket contains more than one elements(with same hashcode value),How can differentiate the elements ?
OR
Is it possible more than one elemets has same hashcode ?
In general, there are two ways to handle the situation where two or more elements end up in the same "bucket" in a hash table:
A bucket can be a linked list (or equivalent) and hold lots of elements. This is how HashMap works ... though in Java 8, the linked list can be a binary tree.
A bucket can have a bounded size, and then "overflow" into another one.
My doubt is if the bucket contains more than one elements(with same hashcode value). How can differentiate the elements ?
In Java, the HashMap, Hashtable and related classes, use the key's equals method to differentiate keys with the same hashcode. That's why we have the "hashcode / equals contract". (Read about it here.)
Is it possible more than one elemets has same hashcode?
Yes it is possible. Java's standard hash table classes cope with it. See above.
Multiple elements can have the same hashcode. In this case all the elements in that bucket will be checked using the equals() method to determine if one of them matches the one you are looking for.
It is possible to have more than one element with same hashcode cause count of hashcode is limited set. And you can differentiate the elements via equals() method.

What do HashMap and HashSet have in common? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Everywhere you can find answer what are differences:
Map is storing keys-values, it is not synchronized(not a thread safe), allows null values and only one null key, faster to get value because all values have unique key, etc.
Set - not sorted, slower to get value, storing only value, does not allow duplicates or null values I guess.
BUT what means Hash word (that is what they have the same). Is it something about hashing values or whatever I hope you can answer me clearly.
Both use hash value of the Object to store which internally uses hashCode(); method of Object class.
So if you are storing instances of your custom class then you need to override hashCode(); method.
HashSet and HashMap have a number of things in common:
The start of their name - which is a clue to the real similarity.
They use Hash Codes (from the hashCode method built into all Java objects) to quickly process and organize Objects.
They are both unordered collections - but both provide ordered varients (LinkedHashX to store objects in the order of addition)
There is also TreeSet/TreeMap to sort all objects present in the collection and keep them sorted. A comparison of TreeSet to TreeMap will find very similar differences and similarities to one between HashSet and HashMap.
They are also both impacted by the strengths and limitations of Hash algorithms in general.
Hashing is only effective if the objects have well behaved hash functions.
Hashing breaks entirely if equals and hashCode do not follow the correct contract.
Key objects in maps and objects in set should be immutable (or at least their hashCode and equals return values should never change) as otherwise behavior becomes undefined.
If you look at the Map API you can also see a number of other interesting connections - such as the fact that keySet and entrySet both return a Set.
None of the Java Collections are thread safe. Some of the older classes from other packages were but they have mostly been retired. For thread-safety look at the concurrent package for non-thread-safety look at the collections package.
Just look into HashSet source code and you will see that it uses HashMap. So they have the same properties of null-safety, synchronization etc:
public class HashSet<E>
...
private transient HashMap<E,Object> map;
// Dummy value to associate with an Object in the backing Map
private static final Object PRESENT = new Object();
/**
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
* default initial capacity (16) and load factor (0.75).
*/
public HashSet() {
map = new HashMap<>();
}
...
public boolean contains(Object o) {
return map.containsKey(o);
}
...
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
...
}
HashSet is like a HashMap where you don't care about the values but about the keys only.
So you care only if a given key K is in the set but not about the value V to which it is mapped (you can think of it as if V is a constant e.g. V=Boolean.TRUE for all keys in the HashSet). So HashSet has no values (V set). This is the whole difference from structural point of view. The hash part means that when putting elements into the structure Java first calls the hashCode method. See also http://en.wikipedia.org/wiki/Open_addressing to understand in general what happens under the hood.
The hash value is used to check faster if two objects are the same. If two objects have same hash, they can be equal or not equal (so they are then compared for equality with the equals method). But if they have different hashes they are different for sure and the check for equality is not needed. This doesn't mean that if two objects have same hash values they overwrite each other when they are stored in the HashSet or in the HashMap.
Both are not Thread safe and store values using hashCode(). Those are common facts. And another one is both are member of Java collection framework. But there are lots of variations between those two.
Hash regards the technique used to convert the key to an index. Back in the data strucutures class we used to learn how to construct a hash table, to do that you would need to get the strings that were inserted as values and convert them to a number to index an array used internally as the storing data structure.
One problem that was also very discussed was to find a hashing function that would incurr in minimum colision so that we won't have two different objects, with different keys sharing the same position.
So, the hash is about how the keys are processed to be stored. If we think about it for a while, there isn't a (real) way to index memory with strings, only with numbers, so to have a 2d structure like a table that is indexed by a string (or an object as you wish) you need to generate a number (or a hash) for that string and store the value in an array in this index. However, if you need the key "name" you would need a different array to, in the same index, store the key "name".
Cheers
The "HASH" word is common because both uses hashing mechanism. HashSet is actually implemented using HashMap, using dummy object instance on every entry of the Set. And thereby a wastage of 4 bytes for each entry.

Locate Objects in Collection with multiple keys [duplicate]

This question already has answers here:
How to implement a Map with multiple keys? [duplicate]
(27 answers)
Closed 9 years ago.
Have a class with attributes attr1, attr2, attr3 and attr4.
I am looking for a way to load a bunch of objects of that class in an array (or ArrayList or Map or whatever) and then being able to retrieve (search) these objects based on attr1 or attr2.
It looks like the way to go is HashMap but I will have to:
if I want only one HashMap, I will have two entries for each object, one with key attr1 and one with key attr2
have two HashMap objects, one with key attr1 and the other with key attr2 and based on what I am searching for, use the appropriate Map.
Is there any other elegant way of doing this? Is there a Map or Collection object that will allow me to provide multiple keys for an object?
I find the second solution with two Map objects quite elegant, each Map being a sort of index of your data. If you really want one single structure, then you can use Guava's Table, who is mapping values to a pair of keys.

Categories