0

How do I print out the key of a maximum value it a hashmap? Check the code in multiline comment

Used: Collections.max(players.getKey); But got errors import java.util.*; public class Bowling { HashMap<String, Integer> players; Bowling() { players = new HashMap<String, Integer>(); } public void addPlayer(String name, int p) { players.put(name, p); } /** public void getWinner() { for (Map.Entry entryPair: players.entrySet()){ int maxV = Collections.max(players.Values); System.out.println(maxV(players.getKey)); } }*/ } public class Program { public static void main(String[ ] args) { Bowling game = new Bowling(); Scanner sc = new Scanner(System.in); for(int i=0;i<3;i++) { String input = sc.nextLine(); String[] values = input.split(" "); String name = values[0]; int points = Integer.parseInt(values[1]); game.addPlayer(name, points); } game.getWinner(); } }

23rd Jul 2021, 7:15 AM
Ndeji Chiputa
Ndeji Chiputa - avatar
2 odpowiedzi
0
Use this: public void getWinner(){ int maxScore = Collections.max(players.values()); for(Map.Entry player: players.entrySet()){ if(player.getValue() == maxScore){ System.out.println(player.getKey()); } } }
23rd Jul 2021, 7:38 AM
Baribor Saturday
Baribor Saturday - avatar
0
I've tried to use this, its says “Reached end of file while parsing”
23rd Jul 2021, 9:34 AM
Ndeji Chiputa
Ndeji Chiputa - avatar