Question: I have a map structured as follows

`Map<String, Set<String> myMapOfSets = new HashSet<>():`

This map is of size 400. And each of these 400 sets can be of size anywhere between 100 to 10000 elements. Many elements in these sets are repeating. I am looking to find say top 10 elements which are most popular in these 400 sets. How to implement that? Thanks for any help.

Answer: 
Using Java 8 streams:

```
Map<String, Set<String>> myMapOfSets = new HashMap<>();
myMapOfSets.put("K1", new HashSet<>(Arrays.asList("A", "B", "C",      "E"     )));
myMapOfSets.put("K2", new HashSet<>(Arrays.asList(     "B", "C", "D",      "F")));
myMapOfSets.put("K3", new HashSet<>(Arrays.asList("A",      "C",      "E", "F")));
myMapOfSets.put("K4", new HashSet<>(Arrays.asList(     "B", "C", "D"          )));
myMapOfSets.put("K5", new HashSet<>(Arrays.asList("A",      "C", "D"          )));
myMapOfSets.put("K6", new HashSet<>(Arrays.asList(     "B", "C", "D", "E", "F")));

List<Entry<String, Long>> result =           // change to List<String> if you only want values
        myMapOfSets.values()
                   .stream()
                   .flatMap(Set::stream)
                   .collect(Collectors.groupingBy(s -> s, Collectors.counting()))
                   .entrySet()
                   .stream()
                   .sorted((e1, e2) -> Long.compare(e2.getValue(), e1.getValue())) // descending
                   .limit(3)                 // change to 10 for your code
                // .map(Map.Entry::getKey)   // uncomment if you only want values
                   .collect(Collectors.toList());
result.forEach(System.out::println);

```

*Output*

```none
C=6
B=4
D=4

```
If this is homework related, here is some tips:

Assuming the map can be treated as a set of sets, and that the string keys arn't related.

```
Create a new map called scores, store strings and ints
Iterate over the sets
   Iterate over the values of a set
     get the value of the string from scores defaulting to 0, and add 1.

```

You now have a map of occurrences, 

```
create a list of mapentries of length 10, keep it sorted.
Loop over the map
   if the current value is higher then the min value in the list, replace the lowest value, and keep track of the new minimumscore.

```