I have something like below written as an "old-school" version where I am trying to collect and group similarities in a given List
. I would like to transfer this solution into, I think better, functional
way with grouping
or something more java 8 stream
way.
Actually I would like to get rid of initialization for List<List<T>> listOfLists = new LinkedList<List<T>>();
and passing of this instance to getMatchedList
method.
Here is my current solution:
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
public class HelloWorld {
public static void main(String... args) {
List<Integer> originalList = Arrays.asList(1, 2, 4, 5, 7);
List<List<Integer>> listOfLists = new LinkedList<>();
originalList.stream()
.forEach(item -> {
Optional<List<Integer>> list = getMatchedList(item, listOfLists);
if (list.isPresent()) {
list.get().add(item);
} else {
listOfLists.add(new LinkedList<>(Arrays.asList(item)));
}
});
System.out.println(listOfLists);
}
static Optional<List<Integer>> getMatchedList(Integer item, List<List<Integer>> list) {
return list.stream().filter(a -> matched(a, item)).findAny();
}
static boolean matched(List<Integer> list, Integer b) {
return list.stream().filter(x -> isSimilar(x, b)).findAny().isPresent();
}
static boolean isSimilar(Integer a, Integer b) {
return Math.abs(a - b) <= 1; // true or false based on additional logic
}
}
Let's say isSimilar function is Math.abs(a - b) <= 1
.
As a result I would like to have list of lists as follow:
[
[1,2],
[4,5],
[7]
]
Note: For example if we have 1,2,3
we should have list: [1,2,3]
despite of that 1
and 3
are not similar. But they are "connected" with common similarity which is 2
.
Note II: The question is not about the numbers. Numbers are just for simplicity. Actually I have custom objects not Integers
and I also have different isSimilar
function. The goal is to achieve this "grouping" to clusters in java-8 functional
way.