I have the following imperative programming code snippet here:
Map<String, Integer> nameCounts = new ConcurrentHashMap<>();
String mostCommon = null;
int mostCommonCount = -1;
for (Map.Entry<String, Integer> entry : nameCounts.entrySet()) {
if (mostCommon == null || entry.getValue() > mostCommonCount) {
mostCommon = entry.getKey();
mostCommonCount = entry.getValue();
}
}
When executed with certain data, nameCounts
should contain results such as:
nameCounts = {HashMap@795} size = 6
0 = {HashMap$Node@805} "William" -> "232818"
1 = {HashMap$Node@806} "Dennis" -> "233249"
2 = {HashMap$Node@807} "Maynard" -> "233205"
3 = {HashMap$Node@808} "Vincent" -> "233288"
4 = {HashMap$Node@809} "John" -> "234109"
5 = {HashMap$Node@810} "Donald" -> "233331"
What is the functional approach by using Java 8's Streams I could use, to get the String
with the highest value? (in this case, it would be "John").
Thanks in advance!