I am trying java lambda expresssions to remove duplicate without using distinct.
Here is my solution:
public static List<Integer> dropDuplicates(List<Integer> list) {
return list
.stream()
.collect(Collectors.groupingBy(Function.identity()))
.values()
.stream()
.map(v -> v.stream().findFirst().get())
.collect(toList());
}
It is working fine but order of elements are changed.
List<Integer> list = Arrays.asList(11, 12, 1, 2, 2, 3,12, 4, 13, 4, 13);
output => [1, 2, 3, 4, 11, 12, 13]
I am bit new to java functional programming(maybe this is stupid question). Is any way to preserve order of the list elements or other better way to do this?