问题描述
据我了解,以下代码应打印 true
,因为 Stream
和 Iterator
指向第一个元素。
然而,当我运行以下代码时,它正在打印 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);
两个条目都指的是你的相同逻辑条目地图(其键为A,值为B)。但是,它们不是同一个实例。
如果你深入挖掘 Collections.unmodifiableMap(map)$ c $的实现c>你会看到迭代
Collections.unmodifiableMap(map)
返回的地图的 entrySet
返回一个新的 Map.Entry
包装原始可修改条目:
公共地图.Entry< K,V> next(){
返回new UnmodifiableEntry<>(i.next());
}
我假设一个新实例 Map.Entry当你调用
实例,所以这两个方法返回不同的实例。 / p> set.stream()。findFirst()。get()
时,也会创建
即使您将两次调用相同的方法,您也会获得差异实例,即以下代码也将打印 false
:
Map.Entry< String,String> entry1 = set.iterator()。next();
Map.Entry< String,String> entry2 = set.iterator()。next();
System.out.println(entry1 == entry2);
另一方面,如果您直接从原始 HashMap获取条目
,您将获得 true
:
Map.Entry< String,String> entry1 = map.entrySet().iterator()。next();
Map.Entry< String,String> entry2 = map.entrySet()。stream()。findFirst()。get();
System.out.println(entry1 == entry2);
如果是这种情况,条目不会被新实例包装,那么 entrySet().iterator()。next()
和 entrySet()。stream()。findFirst()。get()
返回相同的实例。
To my understanding, the following code should print true
, since both Stream
and Iterator
are pointing to the first element.
However, when I run 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);
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<K,V> 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<String, String> entry1 = set.iterator().next();
Map.Entry<String, String> 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<String, String> entry1 = map.entrySet ().iterator().next();
Map.Entry<String, String> 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.
这篇关于Set中的流与迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!