Iterating over EnumMap#entrySet

前端 未结 3 2394
悲哀的现实
悲哀的现实 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

3条回答
  •  执笔经年
    2021-02-20 18:47

    EnumMap.EntryIterator.next() returns this reference. You can verify it as follows:

    Iterator> e = map.entrySet().iterator();
    while (e.hasNext()) {
        Map.Entry x = e.next();
        System.out.println(System.identityHashCode(x));
    }
    

提交回复
热议问题