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

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

    If anyone needs this yet, there is StreamEx.zipWith function in streamex library:

    StreamEx givenNames = StreamEx.of("Leo", "Fyodor")
    StreamEx familyNames = StreamEx.of("Tolstoy", "Dostoevsky")
    StreamEx fullNames = givenNames.zipWith(familyNames, (gn, fn) -> gn + " " + fn);
    
    fullNames.forEach(System.out::println);  // prints: "Leo Tolstoy\nFyodor Dostoevsky\n"
    

提交回复
热议问题