Stream Vs. Iterator in entrySet of a Map - Java 8

后端 未结 3 1841
轻奢々
轻奢々 2021-02-18 17:10

To my understanding, the following code should have print true, since both Stream and Iterator are pointing to the first element.

3条回答
  •  面向向阳花
    2021-02-18 17:58

    Both entries are referring to the same logical entry of your Map (whose key is "A" and value is "B"). However, they are not the same instance.

    If you dig deep enough in the implementation of Collections.unmodifiableMap(map) you'll see that iterating over the entrySet of the map returned by Collections.unmodifiableMap(map) returns a new Map.Entry which wraps the original modifiable entry:

    public Map.Entry next() {
      return new UnmodifiableEntry<>(i.next());
    }
    

    I'm assuming a new instance Map.Entry instance is also created when you call set.stream().findFirst().get(), so the two methods return different instances.

    Even if you'll call the same method twice you'll get difference instances, i.e. the following code will also print false:

    Map.Entry entry1 = set.iterator().next();
    Map.Entry entry2 = set.iterator().next();
    System.out.println(entry1 == entry2);
    

    On the other hand, if you obtain the entry directly from the original HashMap, you will get true:

    Map.Entry entry1 = map.entrySet ().iterator().next();
    Map.Entry entry2 = map.entrySet ().stream().findFirst().get();
    System.out.println (entry1==entry2);
    

    If this case the entry is not wrapped by a new instance, so both entrySet ().iterator().next() and entrySet ().stream().findFirst().get() return the same instance.

提交回复
热议问题