Skip to content

Instantly share code, notes, and snippets.

@tomaszalusky
Last active August 29, 2015 14:13
Show Gist options
  • Save tomaszalusky/c169dd2c4b0cbdc949a6 to your computer and use it in GitHub Desktop.
Save tomaszalusky/c169dd2c4b0cbdc949a6 to your computer and use it in GitHub Desktop.
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
import java.util.List;
import java.util.Arrays;
import com.google.common.collect.Lists;
// javac -classpath c:\java\m2\repository\com\google\guava\guava\17.0\guava-17.0.jar X.java
// java -classpath .;c:\java\m2\repository\com\google\guava\guava\17.0\guava-17.0.jar X
public class X {
static class Pair<T1,T2> {
private final T1 first;
private final T2 second;
public static <T1,T2> Pair<T1,T2> of(T1 first, T2 second) {
return new Pair<T1,T2>(first,second);
}
private Pair(T1 first, T2 second) {
this.first = first;
this.second = second;
}
@Override
public String toString() {
String retval = String.format("(%s,%s)",first,second);
return retval;
}
}
public static void main(String... args) {
List<String> list = Arrays.asList("D","A","G","I"); // assuming even size for simplicity
List<Pair<?,?>> result = Lists.partition(list,2).stream().map(l -> Pair.of(l.get(0),l.get(1))).collect(toList());
System.out.println(result); // [(D,A), (G,I)]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment