我在这里有以下命令式编程代码片段:
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();
}
}
使用某些数据执行时,nameCounts
应包含以下结果:
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"
使用Java 8的Streams我可以使用什么功能方法来获得具有最高值的String
? (在这种情况下,它将是“约翰”)。
提前致谢!