+ 1
Hashmap and hashset difference?
4 odpowiedzi
+ 1
HashSet is implementation of Set Interface which does not allow duplicate value. The main thing is, objects that are stored in HashSet must override equals() for check for equality and hashCode() methods for no duplicate value are stored in our set.
HashMap is an implementation of Map Interface, which map a key to value. Duplicate keys are not allowed in a map.Basically Map Interface has two implementation classes HashMap and TreeMap the main difference is TreeMap maintains order of the objects but HashMap will not.HashMap allows null values and null keys.
Both HashSet and HashMap are not synchronized.
1) Implementation: HashMap implements Map interface and HashSet implements Set interface.
2) Duplicates: HashSet does’t allow duplicate values. HashMap store key, value pairs and it does not allow duplicate keys. If key is duplicate then old key is replaced with new value.
+ 1
3) Number of objects during storing objects : HashMap requires two objects put(K key, V Value) to add an element to HashMap object, while HashSet requires only one object add(Object o) .
4) Dummy value : In HashMap no concept of dummy value,
HashSet internally uses HashMap to add elements. In HashSet, the argument passed in add(Object) method serves as key K. Java internally associates dummy value for each value passed in add(Object) method.
5) Storing or Adding mechanism : HashMap internally uses hashing to store or add objects, HashSet internally uses HashMap object to store or add the objects.
6) Faster:HashSet is slower then HashMap.
+ 1
7) Insertion HashMap use put() method for storing data, While in HashSet use add() method for add or storing data.
8) Example: HashSet is a set, e.g. {1, 2, 3, 4, 5, 6, 7},
HashMap is a key -> value pair(key to value) map, e.g. {a -> 1, b -> 2, c -> 2, d -> 1}
Notice in my example above that in the HashMap there must not be duplicate keys, but it may have duplicate values.
In the HashSet, there must be no duplicate elements.
0
Thanks will do work out