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.

2
  • 3
    Check this: 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(). Commented May 14, 2015 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. Commented May 14, 2015 at 18:35

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.