Skip to content

Instantly share code, notes, and snippets.

@qianyan
Last active October 13, 2017 03:02
Show Gist options
  • Save qianyan/c68b8f188f0fd17193e46a294157fd31 to your computer and use it in GitHub Desktop.
Save qianyan/c68b8f188f0fd17193e46a294157fd31 to your computer and use it in GitHub Desktop.
implement frequency and unique? in java8
// frequency
Stream.of("a", "a", "b", "c").collect(toMap(identity(), a -> 1, (a, b) -> a + b));
Stream.of("a", "a", "b", "c").collect(groupingBy(identity(), counting()));
Stream.of("a", "a", "b", "c").collect(groupingBy(identity(), summingInt(v -> 1)));
Stream.of("a", "a", "b", "c").collect(groupingBy(identity(), mapping(identity(), summingInt(v -> 1))));
-> {a=2, b=1, c=1}
// unique?
frequency().values().stream().anyMatch(c -> c > 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment