0

I am trying to make a HashMap such that the keys are months of the year and the values are names of peoples who's birthday it is in that month. I am quite stuck and don't know exactly what's wrong. Help is much appreciated.

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class BirthdayStore {

HashMap<String, List<String>> map = new HashMap<String, List<String>>();

public BirthdayStore() {
    HashMap<String, List<String>> map = new HashMap<String, List<String>>();
}

public boolean containsKey(String key) {
    if(map.containsKey(key)) {
        return true;

    }
    return false;
}

public void put(String key, String word) {
    if(!map.containsKey(key)) {
        ArrayList<String> arraylist = new ArrayList<String>();
        arraylist.add(word);
    }
    else{
        ArrayList<String> arraylist = (ArrayList<String>) map.get(key);
        arraylist.add(word);

    }
    }
public List<String> get(String key) {
    return map.get(key);
}
public static void main(String[] args) {
    BirthdayStore k = new WordStore();
    k.put("september","jack" );
    k.put("september","josh" );
    k.put("january","james");

    System.out.println(k.get("september"));
}
}

Currently, my output is null.

braaterAfrikaaner
  • 1,072
  • 10
  • 20
boys1
  • 3
  • 2

2 Answers2

1

In addition to @Raizuri's answer, just let you know that there is a quite useful method for HashMaps, getOrDefault, which retrieves the value for a key and lets you define a default value which is returned in case your key is absent in the map. This way you don't have to use the conditional case:

public void put(String key, String word) {
    List<String> monthBirthdays = map.getOrDefault(key, new ArrayList<>());
    monthBirthdays.add(word);
    map.put(key, monthBirthdays);
}
stjernaluiht
  • 730
  • 6
  • 14
  • Why do you use List instead of ArrayList on the second line? – boys1 Feb 14 '18 at 22:17
  • Sorry if that was confusing, I think it's a bit subjective. `List` is an interface that `ArrayList` implements, so the benefits of using a more generic declaration type is that you could change your implementation to use `LinkedList` (which also implements `List`) and you wouldn't have to change the declaration. You also depend less on specific `ArrayList` implementation details, which is generally good. You can read more [here](https://stackoverflow.com/questions/12321177/arraylist-or-list-declaration-in-java). – stjernaluiht Feb 14 '18 at 22:29
0

The problem is that when you call your put() method, you create the arraylist and all, but you don't put that arraylist into your map.

public void put(String key, String word) {
    if(!map.containsKey(key)) {
        ArrayList<String> arraylist = new ArrayList<String>();
        arraylist.add(word); // <-- here you made a new arraylist and added your word to it, but what are you doing with this array list? 
                             // You're not putting it into the HashMap for this key
        map.put(key, arraylist); // <-- you have to remember to actually put the arraylist into the map! 
    }
    else{
        ArrayList<String> arraylist = (ArrayList<String>) map.get(key);
        arraylist.add(word);

    }
}
aphrid
  • 589
  • 8
  • 25
  • Ahh man I feel so dumb. Well at least I'm learning. Thank you so much for your response, very clear and the comments helped a lot. Much love. – boys1 Feb 14 '18 at 22:01
  • No problem! If you wouldn't mind, could you please mark the solution as accepted to help provide some closure to the question? Thanks and good luck with learning Java! – aphrid Feb 14 '18 at 22:13