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

前端 未结 14 1383
旧巷少年郎
旧巷少年郎 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:54

    zip is one of the functions provided by the protonpack library.

    Stream streamA = Stream.of("A", "B", "C");
    Stream streamB  = Stream.of("Apple", "Banana", "Carrot", "Doughnut");
    
    List zipped = StreamUtils.zip(streamA,
                                          streamB,
                                          (a, b) -> a + " is for " + b)
                                     .collect(Collectors.toList());
    
    assertThat(zipped,
               contains("A is for Apple", "B is for Banana", "C is for Carrot"));
    

提交回复
热议问题