1

I've been given a test-driven development problem (I need to make it work based on the junit methods provided) based on implementing a HashMap that uses a strings for the keys and ArrayLists for the values. The key needs to be able to support one or more corresponding values. I need to set up my methods in a way that I can add or subtract values from the hash, and then see the updated contents of the hash. My struggle is taking info provided from the unit method shown below (exercising myClass and it's addingMethod method) methods) and getting it put properly into the hash.

void add() {
    myClass = new MyClass("key1", "value1");
    myClass.addingMethod("blargh", "blarghvalue");
    myClass.addingMethod("blargh2", "uglystring");
    myClass.addingMethod("blargh", "anotherstring");
    //and so on and so on............

For my end result, when I print out the results of myClass, I need to see something like: {blargh=[blarghvalue, anotherstring], blargh2=uglystring}

I need to be able to add to this, and remove values as well.

I'm very new to java collections (obviously). I can get things to work if they only have a 1 to 1 relationship, and the hashmap is 1:1. So a very simple addingMethod like this:

public void addingMethod(String key, String value) {
    hashMap.put(key, value);

Will get a string string hashmap, but of course if I reuse a key with a new key-value pair, the original key-value gets stepped on and goes away. When it comes to working with hashmaps and arraylists dynamically though, and beyond a 1:1 key:value relationship, I'm lost.

2 Answers 2

3

It sounds like you need a MultiMap, which is provided by Google's great Guava libraries:

A collection similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values.

Sign up to request clarification or add additional context in comments.

4 Comments

Why do people keep linking to the docs for Guava version 3? We're on 13 now.
@LouisWasserman: Because they are the first hit when googling for example Java Guava Multimap, it's easy to miss.
Let me say, as a total java novice, this is a fantastic discovery (although surely a given for all the vets out there). Thank you so much, I just saved several lines of code and a headache. Although I would like to figure out the 'other' method of implementing it as well, to strengthen my java underpinnings.
@City17Mogul: Yes, using well-tested utility projects like Guava and Apache Commons will help you a lot. Look at AmitD:s answer for one way of implementing it manually.
3

What you need is Map<String, List<String>> and inside put check if entry exists if yes then add to list.

Declare your Map like below

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

Note that syntax of adding Method should be same as put

public List<String> put(String key, String value) {
    if (!map.containsKey(key)) {
        return map.put(key, new ArrayList<String>());
    }
    List<String> list = map.get(key);
    list.add(value);
    return map.put(key, list);
}

2 Comments

this is awesome, but how to print it?
you saved my day

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.