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


i have hashmap called prizewinners. key each pair in hashmap year nobel prize won. value name(s) of winners. value array of strings, because in given prize year, there can 3 winners. question this: given name of winner, return year in won prize. interestingly enough, return value needs string: public string getyearwon(string name), name name of 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 stuck: cannot seem access array correctly iterate through , key. there several methods in assignment requiring me (for example, print out names of winners), hashmap address, not contents of array itself.

iterate through prizewinner keys (years). each year, winners string array , convert list, call contains method see if winners list contains winner name passed parameter. if it's case, stop looping , return year string. if went through years didn't find winner's name, 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) {     (int year : prizewinners.keyset()) {         if (arrays.aslist(prizewinners.get(year)).contains(name)) {             return string.valueof(year);         }     }     return null; } 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -