Created
January 5, 2018 05:41
-
-
Save nakaly/aafc404d18d8addba7ec057faf47bd7b to your computer and use it in GitHub Desktop.
MakingProgramingGroup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
import java.util.stream.Stream; | |
public class MakingProgramingGroup { | |
public static void main(String... args) { | |
List<String> allMembers = Arrays.asList("@mrt51", "@aidi", "@veronica", "@ukitaka", "@nakaly", "@yoneharamasami", "@chens", "dummy"); | |
pairs(allMembers).forEach(group -> System.out.println(group.stream() | |
.map(pair -> pair.stream() | |
.collect(Collectors.joining( | |
" & "))) | |
.collect(Collectors.joining(" , ")))); | |
} | |
private static <T> List<List<List<T>>> pairs(List<T> list) { | |
T first = list.get(0); | |
List<T> rest = list.subList(1, list.size()); | |
return IntStream.range(0, rest.size()) | |
.mapToObj(i -> Stream.concat(Stream.of(Arrays.asList(first, rest.get(i))), | |
IntStream.range(1, list.size() / 2) | |
.mapToObj(j -> pair(rest, i, j))) | |
.collect(Collectors.toList())) | |
.collect(Collectors.toList()); | |
} | |
private static <T> List<T> pair(List<T> list, int index, int offset) { | |
return Arrays.asList(list.get((index - offset + list.size()) % list.size()), | |
list.get((index + offset) % list.size())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment