Given that I have some function that takes two parameters and returns one value , is it possible to convert a Map to a List in a Stream as a non-terminal operation? The nearest I cam find is to use forEach on the map to create instances and add them to a pre-defined List, then start a new Stream from that List. Or did I just miss something?
假设我有一个函数,它接受两个参数并返回一个值,那么是否可以将映射转换为流中的列表,作为非终端操作?最近的I cam find是在map中使用forEach来创建实例并将它们添加到预定义的列表中,然后从该列表开始新的流。还是我错过了什么?
Eg: The classic "find the 3 most frequently occurring words in some long list of words"
经典的“在一长串单词中找到最常出现的三个单词”
wordList.stream().collect(groupingBy(Function.identity, Collectors.counting))).
(now I want to stream the entrySetof that map)
(现在我想要流进地图的入口)
sorted((a,b) -> a.getValue().compareTo(b.getValue))).limit(3).forEach(print...
1 个解决方案
#1
50
You should get the entrySet
of the map and glue the entries to the calls of your binary function:
您应该得到映射的entrySet,并将条目粘贴到二进制函数的调用中:
inputMap.entrySet().stream().map(e->myFun(e.getKey(),e.getValue()));
The result of the above is a stream of T
instances.
上面的结果是一个T实例流。
Update
Your additional example confirms what was discussed in the comments below: group by
and sort
are by their nature terminal operations. They must be performed in full to be able to produce even the first element of the output, so involving them as non-terminal operations doesn't buy anything in terms of performance/memory footprint.
您的附加示例确认了下面评论中讨论的内容:group by和sort都是由它们的自然终端操作进行的。它们必须被充分地执行,以便能够产生甚至是输出的第一个元素,因此将它们作为非终端操作,在性能/内存占用方面不买任何东西。
It happens that Java 8 defines sorted
as a non-terminal operation, however that decision could lead to deceptive code because the operation will block until it has received all upstream elements, and will have to retain them all while receiving.
Java 8定义了作为非终端操作的排序,但是这个决定可能会导致欺骗性的代码,因为操作会阻塞,直到它接收到所有的上游元素,并且在接收时必须保留它们。
#1
50
You should get the entrySet
of the map and glue the entries to the calls of your binary function:
您应该得到映射的entrySet,并将条目粘贴到二进制函数的调用中:
inputMap.entrySet().stream().map(e->myFun(e.getKey(),e.getValue()));
The result of the above is a stream of T
instances.
上面的结果是一个T实例流。
Update
Your additional example confirms what was discussed in the comments below: group by
and sort
are by their nature terminal operations. They must be performed in full to be able to produce even the first element of the output, so involving them as non-terminal operations doesn't buy anything in terms of performance/memory footprint.
您的附加示例确认了下面评论中讨论的内容:group by和sort都是由它们的自然终端操作进行的。它们必须被充分地执行,以便能够产生甚至是输出的第一个元素,因此将它们作为非终端操作,在性能/内存占用方面不买任何东西。
It happens that Java 8 defines sorted
as a non-terminal operation, however that decision could lead to deceptive code because the operation will block until it has received all upstream elements, and will have to retain them all while receiving.
Java 8定义了作为非终端操作的排序,但是这个决定可能会导致欺骗性的代码,因为操作会阻塞,直到它接收到所有的上游元素,并且在接收时必须保留它们。