Created
May 14, 2017 17:31
-
-
Save subh007/1554ffde74f090d411e27caf8d84fdea to your computer and use it in GitHub Desktop.
This file contains hidden or 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.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.stream.IntStream; | |
import java.util.stream.Stream; | |
import java.util.function.Predicate; | |
public class Main{ | |
public static Stream<List<Integer>> getPairs(List<Integer> a, List<Integer> b){ | |
return a.stream().flatMap(itemA -> b.stream().map(itemB -> Arrays.asList(itemA, itemB))); | |
} | |
public static Stream<List<List<Integer>>> get(List<List<Integer>> dataSet) { | |
return IntStream.range(0, dataSet.size()).boxed() | |
.flatMap(i -> dataSet.subList(i+1, dataSet.size()).stream() | |
.map(secondry -> Arrays.asList(dataSet.get(i), secondry))); | |
} | |
public static void main (String[] args) { | |
List<Integer> list1 = Arrays.asList(22, 23, 210, 221, 231, 236, 237, 251, 254, 278, 300, 316, 320); | |
List<Integer> list2 = Arrays.asList(230); | |
List<Integer> list3 = Arrays.asList(365, 366, 367, 373, 410, 413, 415, 417, 419); | |
List<List<Integer>> dataset = Arrays.asList(list1, list2, list3); | |
// this is the predicate | |
Predicate<List<Integer>> predicate = points -> points.get(1) - points.get(0) == 1 || points.get(0) - points.get(1) == 1; | |
get(dataset).flatMap(datapair -> getPairs(datapair.get(0), datapair.get(1))) | |
.filter(predicate).forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment