Given the following list as example :
List<String> input = List.of("FOO", "FOO", "FOO", "FOO", "FOO", "BAR", "BAR", "BAZ", "BAZ", "BAZ", "DOO", "DOO");
I need to get the relative Frequency of each element and print it formatted as a string. To do so I have used 2 steps, first create a frequency map and then calculate relative percentage and format:
Map<String, Long> relativeFrequency =
input.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
Map<String, String> relativeFrequency2 = relativeFrequency.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey,
entry -> String.format ( "%.02f%%", entry.getValue() * 100. / input.size() )));
System.out.println(relativeFrequency2);
and get the result
{BAR=16.67%, DOO=16.67%, FOO=41.67%, BAZ=25.00%}
I was told to refactor the above and do it in one go using Collectors.collectingAndThen, but I can't seem to find the right syntax (compile error Operator '*' cannot be applied to '<lambda parameter>', 'double'). Can someone help to correct the following to get the same result as above?
Map<String, String> relativeFrequency3 =
input.stream().collect(Collectors.collectingAndThen(Collectors.groupingBy(Function.identity(), Collectors.counting(),
total -> String.format ( "%.02f%%", total * 100. / input.size() ))
));