title: How does Java determine uniqueness of Hashmap key?
body: <p>I want to maintain a list of objects such that each object in the list is unique.Also I want to retrieve it at one point. Objects are in thousands and I can't modify their source to add a unique id. Also hascodes are unreliable. </p>
  
  <p>My approach was to utilize the key uniqueness of a map. 
  Say a maintain a map like : </p>
  
  <p><code>HashMap&lt;Object,int&gt; uniqueObjectMap</code>.</p>
  
  <p>I will add object to map with as a key and set a random int as value. But how does java determine if the object is unique when used as a key ? </p>
  
  <p>Say,</p>
  
  <pre><code>List listOne;
  List listTwo;
  Object o = new Object;
  listOne.add(o);
  listTwo.add(o);
  
  uniqueObjectMap.put(listOne.get(0),randomInt()); // -- &gt; line 1
  uniqueObjectMap.put(listTw0.get(0),randomInt());  // --&gt; line 2
  </code></pre>
  
  <p>Will line 2 give an unique key violation error since both are referring to the same object o ? </p>
  
  <p><strong>Edit</strong></p>
  
  <p>So if will <code>unqiueObjectMap.containsKey(listTwo.get(0))</code> return true ? How are objects determined to be equal here ? Is a field by field comparison done ? Can I rely on this to make sure only one copy of ANY type of object is maintained in the map as key ? </p>