1

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<Integer, String[]> prizeWinners;

    public NobelPrizeWinners
    {
      prizeWinners = new HashMap<Interger, String[]>();
      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.

sonata p
  • 13
  • 1
  • 3
  • 3
    Check this: http://stackoverflow.com/questions/1066589/iterate-through-a-hashmap. The value is the array, so, when you find your name in one of the arrays return the key as String: key.toString(). – Juan Marcos Armella May 14 '15 at 18:32
  • I understand you're doing this for a homework assignment. When you start working on your own projects, you can store this kind of data (where a key may be mapped to multiple values) using a 'multimap'. Guava provides some [nice implementations](https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Multimap). – dnault May 14 '15 at 18:35

1 Answers1

1

Iterate through the prizeWinner keys (years). For each year, get the winners String array and convert it to a list, then call the contains method to see if the winners list contains the winner name passed as parameter. If it's the case, we stop looping and return the year as a String. If we went through all the years but didn't find the winner's name, we would return null

private HashMap<Integer, String[]> prizeWinners;

public PartsPortlet()
{
    prizeWinners = new HashMap<Integer, String[]>();
    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) {
    for (int year : prizeWinners.keySet()) {
        if (Arrays.asList(prizeWinners.get(year)).contains(name)) {
            return String.valueOf(year);
        }
    }
    return null;
}
Jihed Amine
  • 2,059
  • 19
  • 32
  • Thank you, this works very well. My class hasn't used the Arrays utility yet, however. Although, inspecting it, it solves lot of my problems very neatly. – sonata p May 14 '15 at 18:50