在Map的entrySet中流与Iterator [英] Stream versus Iterator in entrySet of a Map

查看:900
本文介绍了在Map的entrySet中流与Iterator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,以下代码应打印 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);

不同行为的原因是什么?

解决方案

两个条目都指向Map的相同逻辑条目(其键为A,值为B)。但是,它们不是同一个实例。



如果你深入挖掘 Collections.unmodifiableMap(map)你会看到迭代 Collections.unmodifiableMap(map)返回的地图的 entrySet 返回一个新的 Map.Entry 包装原始可修改条目:

 公共地图.Entry< K,V> next(){
返回new UnmodifiableEntry<>(i.next());
}

我假设一个新实例 Map.Entry当你调用 set.stream()。findFirst()。get()时,也会创建实例,所以这两个方法返回不同的实例。 / p>

即使您将两次调用相同的方法,您也会获得差异实例,即以下代码也将打印 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);

What is the reason of the 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 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.

这篇关于在Map的entrySet中流与Iterator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆