Skip to content

Instantly share code, notes, and snippets.

@patricksan
Created August 13, 2019 14:05
Show Gist options
  • Save patricksan/b26c564c43d36f1ce30cde646444d59b to your computer and use it in GitHub Desktop.
Save patricksan/b26c564c43d36f1ce30cde646444d59b to your computer and use it in GitHub Desktop.
Discovery which itens are not duplicated
package com.moogu.restore;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class LessPerformanceApproach {
public static void main(String[] args) {
List<String> letters = Arrays.asList("a", "b", "c", "d", "e", "f", "g", "f", "e", "c", "b");
LessPerformanceApproach main = new LessPerformanceApproach();
System.out.println(main.processStream(letters.stream()));
}
private List<String> processStream(Stream<String> stream) {
List<String> result = new ArrayList<>();
List<String> allValues = stream.collect(Collectors.toList());
for (String value : allValues) {
if (Collections.frequency(allValues, value) == 1) {
result.add(value);
}
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment