title: Java HashSet vs HashMap
body: <p>I understand that <code>HashSet</code> is based on <code>HashMap</code> 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.</p>
  
  <pre><code>    Set testSet = new HashSet&lt;SimpleObject&gt;();
      Map testMap = new HashMap&lt;Integer, SimpleObject&gt;(); 
  
      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());
  </code></pre>
  
  <p>The output is 1 and 1. </p>
  
  <pre><code>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;
   }
  }
  </code></pre>