英文:
Whats the best way to force Streams.zip to iterate over each element (using Guava, Java)
问题
我正在尝试使用 Streams.zip 来创建一个映射(map)。我不关心流返回的结果,我只需要对每个元素执行 lambda 表达式。以下是我的代码:
String x[] = {"a", "b"};
String y[] = {"c", "d"};
var data = new HashMap<String, String>();
var myStream = Streams.zip(Arrays.stream(x), Arrays.stream(y),
(arg1, arg2) -> { data.put(arg1, arg2); return 0; });
var result = myStream.collect(Collectors.toList()); // 最好不要这行
如果没有 collect( ) 这行,我就无法填充映射,那么有没有更简单的方法来实现这一点呢?我尝试过使用 count() 替代 collect,但这会完全绕过 zip 操作。
或者是否有更简单的单行解决方案(不使用 Streams.zip)来从两个列表中填充映射呢?
英文:
I am trying to use Streams.zip to create a map. I don't care about the stream returned result, I just need the lambda to execute for each element. Here is my code
String x[] = {"a", "b"};
String y[] = {"c", "d"};
var data = new HashMap<String, String>();
var myStream = Streams.zip(Arrays.stream(x), Arrays.stream(y),
(arg1, arg2) -> { data.put(arg1, arg2); return 0; };
var result = myStream.collect(Collectors.toList()); // ideally don't want this line
Without the collect( ) line I don't populate the map, so what's simplest way to do this, I tried count() instead of collect but this shortcuts the zip entirely.
Or is there a simpler one line solution without Streams.zip to populate a map from two lists?
答案1
得分: 5
你应该使用 Streams#forEachPair
替代:
String x[] = {"a", "b"};
String y[] = {"c", "d"};
var data = new HashMap<String, String>();
Streams.forEachPair(Arrays.stream(x), Arrays.stream(y), data::put);
System.out.println(data); // {a=c, b=d}
显然,如果你不需要控制 HashMap,你可以直接使用 collect 方法:
String x[] = {"a", "b"};
String y[] = {"c", "d"};
var result = Streams.zip(Arrays.stream(x), Arrays.stream(y), Maps::immutableEntry)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
System.out.println(result); // {a=c, b=d}
英文:
You should use Streams#forEachPair
instead:
String x[] = {"a", "b"};
String y[] = {"c", "d"};
var data = new HashMap<String, String>();
Streams.forEachPair(Arrays.stream(x), Arrays.stream(y), data::put);
System.out.println(data); // {a=c, b=d}
Obviously if you don't need to control HashMap, you can just collect:
String x[] = {"a", "b"};
String y[] = {"c", "d"};
var result = Streams.zip(Arrays.stream(x), Arrays.stream(y), Maps::immutableEntry)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
System.out.println(result); // {a=c, b=d}
答案2
得分: 0
另一种方法是:
String[] keys = {"k1", "k2"};
String[] values = {"v1", "v2"};
Map<String, String> result = IntStream.range(0, keys.length).boxed()
.collect(Collectors.toMap(i -> keys[i], i -> values[i]));
这种方法的优点是在不必在合并条目之前手动创建映射条目的情况下保持无副作用,所有操作由收集器自身处理。
英文:
Yet another approach:
String[] keys = {"k1", "k2"};
String[] values = {"v1", "v2"};
Map<String, String> result = IntStream.range(0, keys.length).boxed()
.collect(Collectors.toMap(i -> keys[i], i -> values[i]));
This has the advantage of being side-effect free while not having to create map entries manually before merging them, all handled by the collector itself.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论