javacollectionsjava-7entryset

Behavior of contains() and remove() on Set which is returned by entrySet()


I have following code. Why contains & remove returns false?

    Map<Integer, String> p = new TreeMap();
    p.put(1, "w");
    p.put(2, "x");
    p.put(3, "y");
    p.put(4, "z");
    System.out.println(p);// {1=w, 2=x, 3=y, 4=z}
    Set s = p.entrySet();
    System.out.println(s);// [1=w, 2=x, 3=y, 4=z]
    System.out.println(s.contains(1));//false
    System.out.println(s.remove(1));//false
    System.out.println(p);// {1=w, 2=x, 3=y, 4=z}
    System.out.println(s);// [1=w, 2=x, 3=y, 4=z]

Solution

  • The entrySet() returns a Set of Map.Entry instances. So your lookup fails, as objects of type Map.Entry<Integer, String> can never be equal to instances of Integer.

    You should pay attention to the generic signatures, i.e.

    Map<Integer, String> p = new TreeMap<>();
    p.put(1, "w");
    p.put(2, "x");
    p.put(3, "y");
    p.put(4, "z");
    System.out.println(p);// {1=w, 2=x, 3=y, 4=z}
    
    Set<Map.Entry<Integer, String>> s = p.entrySet();
    System.out.println(s);// [1=w, 2=x, 3=y, 4=z]
    
    Map.Entry<Integer, String> entry = new AbstractMap.SimpleEntry<>(1, "foo");
    System.out.println(s.contains(entry)); // false (not containing {1=foo})
    
    entry.setValue("w");
    System.out.println(s.contains(entry)); // true (containing {1=w})
    System.out.println(s.remove(entry));// true
    System.out.println(p);// {2=x, 3=y, 4=z}
    System.out.println(s);// [2=x, 3=y, 4=z]
    

    If you want to process keys instead of entries, you have to use keySet() instead:

    Map<Integer, String> p = new TreeMap<>();
    p.put(1, "w");
    p.put(2, "x");
    p.put(3, "y");
    p.put(4, "z");
    System.out.println(p);// {1=w, 2=x, 3=y, 4=z}
    
    Set<Integer> s = p.keySet();
    System.out.println(s);// [1, 2, 3, 4]
    
    System.out.println(s.contains(1)); // true
    System.out.println(s.remove(1));// true
    System.out.println(p);// {2=x, 3=y, 4=z}
    System.out.println(s);// [2, 3, 4]
    

    For completeness, note Map’s 3rd collection view, the values(). Depending on the actual operation, choosing the right view can simplify your operation dramatically.