Simple and Easy :
public static void main(String[] args) {
generatePairs(Arrays.asList(1, 3, 5, 7, 9)).forEach(System.out::println);
}
public static <E> List<Pair<E>> generatePairs(List<E> src) {
if (src.size() < 2) {
throw new IllegalArgumentException("src.size must be greater than 1");
}
return IntStream.range(0, src.size() - 1)
.mapToObj(i -> new Pair<>(src.get(i), src.get(i + 1)))
.collect(Collectors.toList());
}
and the Pair
class :
public final class Pair<E> {
private E left;
private E right;
public Pair() {
}
public Pair(E left, E right) {
this.left = left;
this.right = right;
}
public E getLeft() {
return left;
}
public void setLeft(E left) {
this.left = left;
}
public E getRight() {
return right;
}
public void setRight(E right) {
this.right = right;
}
@Override
public String toString() {
return "{" + left +
", " + right +
'}';
}
}
EDIT :
Here is a complete example :
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
public final class Pair<L, R> implements Comparable<Pair<L, R>> {
private final Comparator<? super L> comparator;
private L left;
private R right;
public Pair() {
comparator = null;
}
public Pair(Comparator<? super L> comparator) {
this.comparator = comparator;
}
public Pair(L left, R right) {
this(left, right, null);
}
public Pair(L left, R right, Comparator<? super L> comparator) {
this.left = left;
this.right = right;
this.comparator = comparator;
}
public L getLeft() {
return left;
}
public void setLeft(L left) {
this.left = left;
}
public R getRight() {
return right;
}
public void setRight(R right) {
this.right = right;
}
@Override
public int hashCode() {
return Objects.hash(left, right);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || !(obj instanceof Pair)) {
return false;
}
Pair that = (Pair) obj;
return Objects.equals(left, that.left) && Objects.equals(right, that.right);
}
@Override
public String toString() {
return "{" + left +
", " + right +
'}';
}
@Override
@SuppressWarnings("unchecked")
public int compareTo(Pair<L, R> o) {
return comparator == null ? ((Comparable<? super L>) left).compareTo(o.left) : comparator.compare(left, o.left);
}
public static abstract class Stream {
private Stream() {
}
public final java.util.stream.Stream<Pair<Integer, Integer>> of(IntStream src) {
return of(src.boxed());
}
public final java.util.stream.Stream<Pair<Long, Long>> of(LongStream src) {
return of(src.boxed());
}
public final java.util.stream.Stream<Pair<Double, Double>> of(DoubleStream src) {
return of(src.boxed());
}
public final <E> java.util.stream.Stream<Pair<E, E>> of(java.util.stream.Stream<E> src) {
return of(src.collect(Collectors.toList()));
}
@SuppressWarnings("all")
public abstract <E> java.util.stream.Stream<Pair<E, E>> of(E... src);
public abstract <E> java.util.stream.Stream<Pair<E, E>> of(List<E> src);
protected static void checkSize(int size) {
if (size < 1) {
throw new IllegalArgumentException("Empty source.");
}
}
protected static <E> E getOrNull(E[] array, int index) {
return index < array.length ? array[index] : null;
}
protected static <E> E getOrNull(List<E> list, int index) {
return index < list.size() ? list.get(index) : null;
}
public static Stream chained() {
return new ChainedPairStream();
}
public static Stream distinct() {
return new DistinctPairStream();
}
}
private static final class ChainedPairStream extends Stream {
@SafeVarargs
public final <E> java.util.stream.Stream<Pair<E, E>> of(E... src) {
int length = src.length;
checkSize(length);
return IntStream.range(0, Math.max(1, length - 1)).mapToObj(i -> new Pair<>(src[i], getOrNull(src, i + 1)));
}
public final <E> java.util.stream.Stream<Pair<E, E>> of(List<E> src) {
int size = src.size();
checkSize(size);
return IntStream.range(0, Math.max(1, size - 1)).mapToObj(i -> new Pair<>(src.get(i), getOrNull(src, i + 1)));
}
}
private static final class DistinctPairStream extends Stream {
@SafeVarargs
public final <E> java.util.stream.Stream<Pair<E, E>> of(E... src) {
int length = src.length;
checkSize(length);
return IntStream.iterate(0, i -> i + 2)
.limit((long) Math.ceil(length / 2.0))
.mapToObj(i -> new Pair<>(src[i], getOrNull(src, i + 1)));
}
public final <E> java.util.stream.Stream<Pair<E, E>> of(List<E> src) {
int size = src.size();
checkSize(size);
return IntStream.iterate(0, i -> i + 2)
.limit((long) Math.ceil(size / 2.0))
.mapToObj(i -> new Pair<>(src.get(i), getOrNull(src, i + 1)));
}
}
public static void main(String[] args) {
Pair.Stream.distinct().of(1, 2, 3, 4, 5, 6).forEach(System.out::println);
Pair.Stream.chained().of(Arrays.asList(1, 3, 5, 7, 9)).forEach(System.out::println);
Pair.Stream.chained().of(Arrays.stream(new int[]{0, 2, 4, 6, 8})).forEach(System.out::println);
Pair.Stream.distinct().of(Arrays.stream(new int[]{0, 2, 4, 6, 8})).forEach(System.out::println);
Pair.Stream.distinct().of(IntStream.range(0, 6)).forEach(System.out::println);
}
}