-1

I'm trying to access to the values of a pair from a hashmap. I have got a Map where Integer is the key and info is the value. I want to iterate through the map and get the specific data field (namePlayer) from the info object.

class info{
    public String namePlayer;
    public String knowledge; 
    public int coins;

    info(String nom,String coneixament,int monedes){
        namePlayer=nom;
        knowledge=coneixament;
        coins=monedes;
    }

    void setMonedes(int monedes){
        coins=monedes;
    }

    void setConeixement(String confianza){
        knowledge=confianza;
    }
    String getConeixament(){
        return knowledge;
    }
    String getNames(){
        return name;
    }
}
public class Memory{
    private Map<Integer,info> k;

    Memory(){
        k= new HashMap<Integer, info>();
    }

    void getValues(){
        Iterator it = _infoPartida.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry)it.next();
            info s = pair.getValues();// error
            String name = s.getNames():
         }
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
bb88
  • 1
  • 2
  • 1
    What is the actual question? getValues() is of type void, so it won't return anything? – JohannisK May 19 '16 at 09:05
  • 1
    First of all, it's `getValue()`, not `getValues()`. Second of all, `getValue()` will return you an `Object`, since you are using raw types. – Eran May 19 '16 at 09:06
  • An entry contains a key and a value, use `pair.getValue()` to get its value. – Arnaud May 19 '16 at 09:06
  • I want to print ONLY the name of the player that has the key 2 for example . – bb88 May 19 '16 at 09:07
  • Then you don't need to iterate over the entire collection. You can .get(7) the value from the HashMap. – JohannisK May 19 '16 at 09:09

5 Answers5

0

You need to add the generics info to your Iterator

void getValues(){
    Iterator<info> it = k.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = it.next();
        info s = pair.getValue();
        String name = s.getNames():
    }
}
Lee
  • 738
  • 3
  • 13
0
  1. class names are upper case in Java (Info - not info)
  2. k is an horrible name for a variable
  3. This is very easy in Java

    for (Info info : k.values()){
        System.out.println(info.getNames();
    }
    

There you go.

For more information how to handle maps please have a look at the dokumentation

Rainer
  • 761
  • 5
  • 20
0
void getValues(){
    Iterator it = _infoPartida.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pair = (Map.Entry)it.next();
        info s = pair.getValues();// error
        String name = s.getNames():
     }
}

should be something like

public String getPlayerName(int index) {
   info i = k.get(index);
   return i.getNames();
}
JohannisK
  • 535
  • 2
  • 10
  • Also, look at the comments and change your syntax... Classes should start with upper names, variable names should describe what they are containing. – JohannisK May 19 '16 at 09:22
0

As Mr. @Lee pointed out.. you have to add generics to your Iterator...
and Map.Entry

void getValues(){
    Iterator<Map.Entry<Integer,info>> it = _infoPartida.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry<Integer,info> pair = it.next();
        info s = pair.getValue();
        String name = s.getNames();
     }
}

Assuming that k is _infoPartida.
Also, consider @Rainer's 3 points.

Pradhan
  • 518
  • 4
  • 14
  • @JohannisK, You are right. It just corrects the `getValues` method. This was posted before he updated his actual requirements. – Pradhan May 20 '16 at 01:45
0

You have two options here. Either loop through using a for loop or an iterator. For loop is easy as shown by @Rainer however Iterator is more powerful because it allows you to remove the element you are one and has more functionality.

Julian
  • 781
  • 1
  • 11
  • 32