Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java HashSet vs HashMap

I understand that HashSet is based on HashMap implementation but is used when you need unique set of elements. So why in the next code when putting same objects into the map and set we have size of both collections equals to 1? Shouldn't map size be 2? Because if size of both collection is equal I don't see any difference of using this two collections.

    Set testSet = new HashSet<SimpleObject>();     Map testMap = new HashMap<Integer, SimpleObject>();       SimpleObject simpleObject1 = new SimpleObject("Igor", 1);     SimpleObject simplObject2 = new SimpleObject("Igor", 1);     testSet.add(simpleObject1);     testSet.add(simplObject2);       Integer key = new Integer(10);      testMap.put(key, simpleObject1);     testMap.put(key, simplObject2);      System.out.println(testSet.size());     System.out.println(testMap.size()); 

The output is 1 and 1.

SimpleObject code  public class SimpleObject {  private String dataField1; private int dataField2;  public SimpleObject(){}  public SimpleObject(String data1, int data2){     this.dataField1 = data1;     this.dataField2 = data2; }  public String getDataField1() {     return dataField1; }  public int getDataField2() {     return dataField2; }  @Override public int hashCode() {     final int prime = 31;     int result = 1;     result = prime * result             + ((dataField1 == null) ? 0 : dataField1.hashCode());     result = prime * result + dataField2;     return result; }  @Override public boolean equals(Object obj) {     if (this == obj)         return true;     if (obj == null)         return false;     if (getClass() != obj.getClass())         return false;     SimpleObject other = (SimpleObject) obj;     if (dataField1 == null) {         if (other.dataField1 != null)             return false;     } else if (!dataField1.equals(other.dataField1))         return false;     if (dataField2 != other.dataField2)         return false;     return true;  } } 
like image 871
IgorDiy Avatar asked Apr 16 '11 20:04

IgorDiy


People also ask

Which is better HashMap or HashSet?

HashMap is faster than HashSet because the values are associated to a unique key. In HashSet , member object is used for calculating hashcode value which can be same for two objects so equals() method is used to check for equality. If it returns false , that means the two objects are different.

What is difference between HashSet and HashMap and Hashtable in Java?

Java HashMap is a hash table based implementation of Map interface. HashSet is a Set. It creates a collection that uses a hash table for storage. HashMap implements Map, Cloneable, and Serializable interface es.

Why does HashSet use HashMap?

HashSet internally uses HashMap to store it's elements. Whenever you create a HashSet object, one HashMap object associated with it is also created. This HashMap object is used to store the elements you enter in the HashSet. The elements you add into HashSet are stored as keys of this HashMap object.

Does HashMap allow duplicate values in Java?

Each key in a HashMap must be unique. When "adding a duplicate key" the old value (for the same key, as keys must be unique) is simply replaced; see HashMap.


2 Answers

The map holds unique keys. When you invoke put with a key that exists in the map, the object under that key is replaced with the new object. Hence the size 1.

The difference between the two should be obvious:

  • in a Map you store key-value pairs
  • in a Set you store only the keys

In fact, a HashSet has a HashMap field, and whenever add(obj) is invoked, the put method is invoked on the underlying map map.put(obj, DUMMY) - where the dummy object is a private static final Object DUMMY = new Object(). So the map is populated with your object as key, and a value that is of no interest.

like image 69
Bozho Avatar answered Sep 21 '22 06:09

Bozho


A key in a Map can only map to a single value. So the second time you put in to the map with the same key, it overwrites the first entry.

like image 41
ColinD Avatar answered Sep 23 '22 06:09

ColinD