I have 10 sets on the input, where each set contains hundreds of items (strings).
What i want:
I want to find items that are shared by at least two sets and sort them by number of occurrences across different sets in descending order.
My approach:
I've created the following code. However, I want to know, if exists a more efficient way to do this...
Map<String, Integer> sharedCounts = new HashMap<>();
for (int i = 0; i < 10; i++) {
Set<String> words = getWords(i);
for (String word : words) {
if (sharedCounts.containsKey(word)) {
sharedCounts.put(word, commons.get(word) + 1);
} else {
sharedCounts.put(word, 1);
}
}
}
Map<String, Integer> sorted = sharedCounts.entrySet().stream()
.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));