Java 8 StreamZipper

The code (it’s a single class) is on pastebin:
https://pastebin.com/R5Sx07HC

This code is from an early build of Java 8 Beta. This was removed later and no zip function exists in the final Java 8 release. Here’s the source I used:
changeset 8789:569088178418.

There’s also a Discussion on Stackoverflow.

I added a very simple “Pair” class. You can just as well remove it and use javatuples instead.

And here’s some Example code:

import ch.claude-martin.zip.StreamZipper;
import ch.claude-martin.zip.StreamZipper.Pair;
// ...
public static void main(String[] args) { 
  Stream<Integer> a = IntStream.of(1, 2, 3).boxed();  
  Stream<String> b = Stream.of("a", "b", "c");  
  Stream<Pair<Integer, String>> zipped
     = StreamZipper.zip(a, b, Pair<Integer, String>::new);  
  System.out.println(zipped.collect(Collectors.toList()));
  // Output:
  // [Pair(1, a), Pair(2, b), Pair(3, c)]}

Leave a Reply

Your email address will not be published. Required fields are marked *