To my understanding, the following code should have print true, since both Stream and Iterator are pointing to the first element.
However, when I ran the following code it is printing false:
final HashMap<String, String> map = new HashMap<>();
map.put("A", "B");
final Set<Map.Entry<String, String>> set = Collections.unmodifiableMap(map).entrySet();
Map.Entry<String, String> entry1 = set.iterator().next();
Map.Entry<String, String> entry2 = set.stream().findFirst().get();
System.out.println(entry1 == entry2);
What could be the reason for this different behavior?
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 theentrySetof the map returned byCollections.unmodifiableMap(map)returns a newMap.Entrywhich wraps the original modifiable entry:I'm assuming a new instance
Map.Entryinstance is also created when you callset.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:On the other hand, if you obtain the entry directly from the original
HashMap, you will gettrue:If this case the entry is not wrapped by a new instance, so both
entrySet ().iterator().next()andentrySet ().stream().findFirst().get()return the same instance.