title: Map doesn't contain key stored with put method java
body: <p>I have some cars that run on generic roads.</p>
  <p><code>Cars</code> can be <code>Yellow</code> or <code>white</code>, each car has an unique <code>id</code> owned by the subclass (yellow or white).</p>
  <p><code>Road</code> contains one car and a starting point and an arrive. Each point is a <code>Coordinate</code> (int x, int y).</p>
  <p>Then I have a method that takes a <code>List</code> of <code>Roads</code> and returns a <code>map</code>.
  Map key is the car that appears in each road, map value is a <code>Set</code> with all the roads run by that car.</p>
  <p>My problem is that after <code>put</code> cars into the map, <code>get</code> method return null so basically it doesn't find the car inserted one second ago.</p>
  <p>These are all classes (it's a little long but easy) :</p>
  <pre><code>public class Coordinate{
      public int x;
      public int y;
      public Coordinate() {
          x=0;
          y=0;
      }
  }
  
  public abstract class Car implements Comparable&lt;Car&gt;{
       public abstract int getId();
       
       @Override
       public int compareTo(Car c) {
           int ret = this.getId()-c.getId();
           if(ret==0) return 1;
           return ret;
       }
  }
  
  public class Yellow extends Car{
      private static int classId;
      private int id;
      public Yellow() {
          this.id=classId;
          classId++;
      }
      
      public int getId() {
          return this.id;
      }
      
  }
  
  public class White extends Car{
      private static int classId;
      private int id;
      public White() {
          this.id=classId;
          classId++;
      }
      
      public int getId() {
          return this.id;
      }
  }
  
  public class Road implements Comparable&lt;Road&gt;{
      Car car;
      Coordinate start;
      Coordinate arrive;
      public Road(Car newCar) {
          car = newCar;
          start = new Coordinate();
          arrive = new Coordinate();
      }
      @Override
      public int compareTo(Road o) {
          return 1;
      }
      
  }
  </code></pre>
  <p>This is the class with the method that takes a list and returns a map.</p>
  <pre><code>public class DoSomething{
      
      public DoSomething() {
          
      }
      
      Map&lt;Car,Set&lt;Road&gt;&gt; listRoadToMap(List&lt;Road&gt; roadsList){
          Map&lt;Car,Set&lt;Road&gt;&gt; car2roads = new TreeMap&lt;&gt;();
          for(Road r : roadsList) {
              Car c = r.car;
              if(car2roads.containsKey(c)) {
                  car2roads.get(c).add(r);
              }
              else {
                  TreeSet&lt;Road&gt; setR = new TreeSet&lt;&gt;();
                  setR.add(r);
                  car2roads.put(c, setR); 
              }
          }
          return car2roads;
      }
  }
  </code></pre>
  <p>This is <code>JUnitTestCase</code> :</p>
  <pre><code>class test {
  
      private DoSomething ds = new DoSomething();
  
      @Before
      public void setUp() {
  
      }
      
      @Test
      public void testTragittoPerAuto() {
          Yellow y1 = new Yellow();
          Yellow y2 = new Yellow();
          Yellow y3 = new Yellow();
          White  w1 = new White();
          
          Road r1 = new Road(y1);
          Road r2 = new Road(y2);
          Road r3 = new Road(y3);
          Road r4 = new Road(w1);
          List&lt;Road&gt; ris = new ArrayList&lt;&gt;();
          ris.add(r1);
          ris.add(r3);
          ris.add(r2);
          ris.add(r4);
          Map&lt;Car,Set&lt;Road&gt;&gt; car2road = new TreeMap&lt;&gt;();
          assertTrue(ds!=null);
          car2road = this.ds.listRoadToMap(ris);
          Set&lt;Car&gt; setCar= new TreeSet&lt;&gt;();
          setCar = car2road.keySet();
          for(Car a : setCar) System.out.print( a.getClass().getSimpleName().toString() + a.getId() + &quot;\n&quot; );
          assertNotNull(car2road);
          assertFalse(car2road.isEmpty());
          assertTrue(setCar.contains(y1));
          assertTrue(car2road.containsKey(y2));
          assertTrue(car2road.containsKey(y3));
          assertTrue(car2road.get(y1).contains(r1));
          assertTrue(car2road.get(y2).contains(r2));
          assertTrue(car2road.get(y3).contains(r3));
      }
  
  
  }
  </code></pre>
  <p>It fails on line <code>assertTrue(setCar.contains(y1))</code> but this is the output of <code>for(Car a : setCar) System.out.print( a.getClass().getSimpleName().toString() + a.getId() + &quot;\n&quot; );</code> that gets all the cars and prints its <code>class</code>+<code>id</code>.</p>
  <p>Output :</p>
  <pre><code>Yellow0
  White0
  Yellow1
  Yellow2
  </code></pre>
  <p>This is weird because when I get all the keys it finds 4 keys, 3 yellow and one white with all ids but when I want to get them I have a failure.</p>
  <p>Any idea?</p>