// use any package you like! package ch.claude-martin.zip; import java.util.Objects; import java.util.Spliterator; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.stream.Stream; import java.util.stream.StreamSupport; /** * 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: changeset * 8789:569088178418. *
* Discussion on Stackoverflow.
*
*/
public final class StreamZipper {
/** A very simple Pair that can be used to zip two streams.
*
* @author Claude Martin */
public static final class Pair {
/** First element. */
public final A first;
/** Second element. */
public final B second;
public Pair(A first, B second) {
super();
this.first = first;
this.second = second;
}
/**
* Scala-style getter for {@link #first}.
*
* @see #first
* @return the first element.
*/
public A _1() {
return this.first;
}
/**
* Scala-style getter for {@link #second}.
*
* @see #second
* @return the second element.
*/
public B _2() {
return this.second;
}
/**
* Applies the given function to both elements of this pair.
*
* @see #uncurry(BiFunction)
* @param
* (a, b) → (b, a)
*
* @return new Pair<>(this.second, this.first)
*/
public Pair swap() {
return new Pair<>(this.second, this.first);
}
/**
* This Pair as an array so that first is on index 0 and second is on index 1.
*
* @return new Object[] { this.first, this.second };
*/
public Object[] toArray() {
return new Object[] { this.first, this.second };
}
/**
* Returns a string representation of this Pair.
*
* @return "Pair(first, second)"
*/
@Override
public String toString() {
return "Pair(" + this.first + ", " + this.second + ")";
}
}
public static Stream