Organizational Research By

Surprising Reserch Topic

Java collection Hashmap: Get key from a given ARRAY of values


I have a hashMap called prizeWinners. The key for each pair in the hashMap is the year a nobel prize was won. The value is the name(s) of the winners. The value is an array of Strings, because in any given prize year, there can be up to 3 winners. My question is this: Given the name of a winner, return the year in which they won the prize. Interestingly enough, the return value also needs to be a string: public String getYearWon(String name), where name is the name of the winner.

public class NobelPrizeWinners
{
    private HashMap prizeWinners;

    public NobelPrizeWinners
    {
      prizeWinners = new HashMap();
      prizeWinners.put(2009, new String[] {"Barack H. Obama"});
      prizeWinners.put(2008, new String[] {"Martti Ahtisaari"};
      prizeWinners.put(2007, new String[] {"IAEA", "Mohamed ElBaradei"});
      //and many more
    }

    public String getYearWon(String name)
    {
       //code here
    }
}
And this is where I get stuck: I cannot seem to access the array correctly to iterate through it and get the key. There are several methods in my assignment requiring me to do so (for example, to print out the names of all winners), but I only get the hashmap address, not the contents of the array itself.
asked Jun 19, 2015 in Core java by rajesh
0 votes
83 views



Related Hot Questions



Government Jobs Opening


...