+ 1

Am confused about hashmap in Java

Am trying to get output of this hasmap question: I have players .put("Dave",64); Players.put("Amy",103): Players.put("Rob",74); Now am ask to output Amy the highest score like this Amy with no score just Amy, When I try changing the key to value it says error k,v cannot change to string Please help this question have taken me not less than 8 hours searching the web for help.

6th Dec 2020, 11:45 AM
Fineb Alex
Fineb Alex - avatar
20 Antworten
+ 5
Fineb Alex check this out it's perfectly working: import java.util.HashMap; import java.util.Map.Entry; public class MyClass { public static void main(String[ ] args) { HashMap<Integer, String> players = new HashMap<Integer, String>(); players.put(74,"Rob"); players.put(154, "Amy"); players.put(103,"Dave"); //keys for(Entry<Integer,String> entry: players.entrySet()){ System.out.println(entry.getKey()); } //values for(Entry<Integer,String> entry: players.entrySet()){ System.out.println(entry.getValue()); } } }
6th Dec 2020, 2:18 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 5
Fineb Alex you need to find max/highest score (key) from the hash map. And print the value associated with that key like (154 is key of Amy) import java.util.HashMap; import java.util.Map.Entry; public class MyClass { public static void main(String[ ] args) { HashMap<Integer, String> players = new HashMap<Integer, String>(); players.put(74,"Rob"); players.put(154, "Amy"); players.put(103,"Dave"); //Getting max key int max = Collection.max(players.getKey()); for(Entry<Integer,String> entry: players.entrySet()){ if(max==entry.getKey()){ System.out.println(entry.getValue()); break; } } } } Or System.out.println(players.getValue(max)); Edit: your key and value pair is placed opposite,according to the Challenge String type in the hashmap is key and Integer type is value.
6th Dec 2020, 2:46 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 3
A colon after Players.put("Amy",103) Better a semicolon
6th Dec 2020, 12:05 PM
Oma Falk
Oma Falk - avatar
+ 2
Try this: for(Entry<String,Integer> entry: players.entrySet()) System.out.println(entry.getKey());
6th Dec 2020, 1:16 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 2
Fineb Alex import this import java.util.Map.Entry;
6th Dec 2020, 1:48 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 1
Please share code
6th Dec 2020, 12:06 PM
Oma Falk
Oma Falk - avatar
+ 1
I Hv been battling with it since ystd
6th Dec 2020, 12:08 PM
Fineb Alex
Fineb Alex - avatar
0
Yes I kn thanks
6th Dec 2020, 12:05 PM
Fineb Alex
Fineb Alex - avatar
0
I can't share code is solo question
6th Dec 2020, 12:07 PM
Fineb Alex
Fineb Alex - avatar
0
I even write my own code by changing the key to value it works but I can't do same in there code
6th Dec 2020, 12:09 PM
Fineb Alex
Fineb Alex - avatar
0
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); } //your code goes here public void getWinner(){ players .put("Dave", 42); players .put("Amy", 103); players .put ("Rob", 64); for (String i:players.keySet()) System.out.println(i); } } 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(); } }
6th Dec 2020, 12:14 PM
Fineb Alex
Fineb Alex - avatar
0
Frogged I HV share please help
6th Dec 2020, 12:15 PM
Fineb Alex
Fineb Alex - avatar
0
I wrote my by changing the key it https://code.sololearn.com/cWO0cv1Sk0Pk/?ref=app
6th Dec 2020, 12:18 PM
Fineb Alex
Fineb Alex - avatar
0
Error cannot find symbol.
6th Dec 2020, 1:27 PM
Fineb Alex
Fineb Alex - avatar
0
Same Result output with this code: for (String I: players.keySet()) System.out.println(i);
6th Dec 2020, 1:56 PM
Fineb Alex
Fineb Alex - avatar
0
The issue here is they want when you can this method game.getwinner(); only only highest score name must display check my code I past above to see how it suppose to be
6th Dec 2020, 2:28 PM
Fineb Alex
Fineb Alex - avatar
0
What they want is wen u call 103 = Amy
6th Dec 2020, 2:30 PM
Fineb Alex
Fineb Alex - avatar
0
hi
7th Dec 2020, 3:00 AM
HICTOR 01
HICTOR 01 - avatar
0
Hi Hector 01
7th Dec 2020, 10:36 PM
Fineb Alex
Fineb Alex - avatar
0
<\>
8th Dec 2020, 4:34 AM
Sharrivannah nannah
Sharrivannah nannah - avatar