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.

2 Answers 2

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

2 Comments

Why do you use List<String> instead of ArrayList on the second line?
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.
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);

    }
}

2 Comments

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.
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!

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.