[java] How can I iterate over the elements in Hashmap?

I want to make a Java game. At first the program asks for the number of the players; after that, it asks for their names. I put their names in a HashMap with an ID and their score. At the end of the game I count the score and I want to put it in the HashMap (the specific score for the specific name). Does anyone know how to do this? This is my code:

Player:

public class Player {

public Player() {
}

public void setScore(int score) {
    this.score = score;
}

public void setName(String name) {
    this.name = name;
}

private String name;
private int score;

public Player(String name, int score) {
    this.name = name;
    this.score = score;
}
 public String getName() { 
     return name;
 }

@Override
public String toString() {
    return "Player{" + "name=" + name + "score=" + score + '}';
}

public int getScore() {
    return score;
}

main:

Scanner scanner = new Scanner(System.in);
HashMap<Integer,Player> name= new HashMap<Integer,Player>();

    System.out.printf("Give the number of the players ");
    int number_of_players = scanner.nextInt();


    for(int k=1;k<=number_of_players;k++)
     {


         System.out.printf("Give the name of player %d: ",k);
         name_of_players= scanner.nextLine();
         name.put(k, new Player(name_of_players,0));//k=id and 0=score

     }


   //This for finally returns the score and          
    for(int k=1;k<=number_of_players;k++)
  {  
      Player name1 = name.get(k);
     System.out.print("Name of player in this round:"+name1.getName());
    ..............
    .............
    int score=p.getScore();
    name.put(k,new Player(name1.getName(),scr));//I think here is the problem

    for(int n=1;n<=number_of_players;n++)//prints all the players with their score
      {

     System.out.print("The player"+name1.getName()+" has "+name1.getScore()+"points");

      }

Does anyone know how can I finally print for example:

"The player Nick has 10 points.
 The player Mary has 0 points."  

Update:

I did this in main(as Jigar Joshi suggest)

 name.put(k,new Player(name1.getName(),scr)); 
 Set<Map.Entry<Integer, Player>> set =   name.entrySet();

 for (Map.Entry<Integer, Player> me : set) 
 { 
 System.out.println("Score :"+me.getValue().getScore() +" Name:"+me.getValue().getName()); 

}

and it prints "Score :0 Name : a Score :4 Name : a" when i put two names of players "a" and "b".I think the problem is here

 name.put(k,new Player(name1.getName(),scr));

How can I put the names in "names_of_players" of my previous for?

This question is related to java

The answer is


Need Key & Value in Iteration

Use entrySet() to iterate through Map and need to access value and key:

Map<String, Person> hm = new HashMap<String, Person>();

hm.put("A", new Person("p1"));
hm.put("B", new Person("p2"));
hm.put("C", new Person("p3"));
hm.put("D", new Person("p4"));
hm.put("E", new Person("p5"));

Set<Map.Entry<String, Person>> set = hm.entrySet();

for (Map.Entry<String, Person> me : set) {
  System.out.println("Key :"+me.getKey() +" Name : "+ me.getValue().getName()+"Age :"+me.getValue().getAge());

}

Need Key in Iteration

If you want just to iterate over keys of map you can use keySet()

for(String key: map.keySet()) {
     Person value = map.get(key); 
}

Need Value in Iteration

If you just want to iterate over values of map you can use values()

for(Person person: map.values()) {

}

Since all the players are numbered I would just use an ArrayList<Player>()

Something like

List<Player> players = new ArrayList<Player>();

System.out.printf("Give the number of the players ");
int number_of_players = scanner.nextInt();
scanner.nextLine(); // discard the rest of the line.

for(int k = 0;k < number_of_players; k++){
     System.out.printf("Give the name of player %d: ", k + 1);
     String name_of_player = scanner.nextLine();
     players.add(new Player(name_of_player,0)); //k=id and 0=score
}

for(Player player: players) {  
    System.out.println("Name of player in this round:" + player.getName());

HashMap<Integer,Player> hash = new HashMap<Integer,Player>();
Set keys = hash.keySet();   
Iterator itr = keys.iterator();

while(itr.hasNext()){
    Integer key = itr.next();
    Player objPlayer = (Player) hash.get(key);
    System.out.println("The player "+objPlayer.getName()+" has "+objPlayer.getScore()+" points");
}

You can use this code to print all scores in your format.


You should not map score to player. You should map player (or his name) to score:

Map<Player, Integer> player2score = new HashMap<Player, Integer>();

Then add players to map: int score = .... Player player = new Player(); player.setName("John"); // etc. player2score.put(player, score);

In this case the task is trivial:

int score = player2score.get(player);