Zipping streams using JDK8 with lambda (java.util.stream.Streams.zip)

前端 未结 14 1562
旧巷少年郎
旧巷少年郎 2020-11-21 23:24

In JDK 8 with lambda b93 there was a class java.util.stream.Streams.zip in b93 which could be used to zip streams (this is illustrated in the tutorial Exploring Java8 Lambda

14条回答
  •  春和景丽
    2020-11-21 23:43

    If you have Guava in your project, you can use the Streams.zip method (was added in Guava 21):

    Returns a stream in which each element is the result of passing the corresponding element of each of streamA and streamB to function. The resulting stream will only be as long as the shorter of the two input streams; if one stream is longer, its extra elements will be ignored. The resulting stream is not efficiently splittable. This may harm parallel performance.

     public class Streams {
         ...
    
         public static  Stream zip(Stream streamA,
                 Stream streamB, BiFunction function) {
             ...
         }
     }
    

提交回复
热议问题