Iterating over EnumMap#entrySet

前端 未结 3 1909
悲哀的现实 2021-02-20 18:06

Enumerating over Map#entrySet doesn\'t work as expected for all Map implementations, specially for EnumMap, IdentityHashMap and here is the sample code

  • 2021-02-20 18:44

    The problem is not with the map but with the implementation of EntryIterator and the HashSet specification that accept only not equal elements.

    In case 1 and 2 maps should have two elements, you can verify that calling


    The 'problem' is in the implementation of EntryIterator by EnumMap class, as this is a puzzle try to figure out itself why.

    ps. use debugger.


    This is what you are really doing is:

        Set<Map.Entry<Sex, Sex>> set =  new HashSet<Map.Entry<Sex, Sex>>();
        Iterator<Map.Entry<Sex, Sex>> e = entrySet.iterator();
        while (e.hasNext()) {

    Remember that HashSet is implemented over HashMap, the values added to hashMap based on hashcode and equality.

    BTW Everything in explained in OP link to the puzzle. The bug is in the equal method that after second invocation of method next(), change the way of working and compare the class type than a values return o == this;.

    0 讨论(0)
  • 2021-02-20 18:45

    Have a look at the implementation. This should be enough to figure out the problem.

    A clue is that the resulting set is:

    [FEMALE=2, FEMALE=2]

    which is not the correct result.

    The effect you see is due to the EnumMap.EntryIterator.hashCode() implementation (which is the Map.Entry here). It's

    h = key ^ value

    This results in the same hash value for the entries produced by

    map.put(Sex.MALE,   Sex.MALE); 
    map.put(Sex.FEMALE, Sex.FEMALE); 

    a stable 0.


    map.put(Sex.MALE,   Sex.FEMALE); 
    map.put(Sex.FEMALE, Sex.MALE);

    here it's an instable (for multiple executions) int value. You will always see the effect if key and value hashs are the same value because: a ^ b == b ^ a. This results in the same hash value for the Entry.

    If entries have the same hash value they end up in the same bucket of the hash table and the equals will always work as they are the same object anyway.

    With this knowledge we can now also produce the same effect with other types like Integer (where we know the hashCode implementation):

    map.put(Sex.MALE,   Integer.valueOf(Sex.MALE.hashCode())); 
    map.put(Sex.FEMALE, Integer.valueOf(Sex.MALE.hashCode()));
    [FEMALE=1671711, FEMALE=1671711]

    Bonus: The EnumMap implementation breaks the equals() contract:

    EnumMap<Sex, Object> enumMap = new EnumMap<Sex, Object>(Sex.class);
    enumMap.put(Sex.MALE, "1");


    Exception in thread "main" java.lang.IllegalStateException: Entry was removed
        at java.util.EnumMap$EntryIterator.checkLastReturnedIndexForEntryUse(
        at java.util.EnumMap$EntryIterator.getValue(
        at java.util.EnumMap$EntryIterator.equals(
        at com.Test.main(
    0 讨论(0)
  • 2021-02-20 18:47 returns this reference. You can verify it as follows:

    Iterator<? extends Map.Entry<Sex, Sex>> e = map.entrySet().iterator();
    while (e.hasNext()) {
        Map.Entry<Sex, Sex> x =;
    0 讨论(0)