Note that there are some explanatory texts on larger screens.

plurals
  1. POHashMap<String, ArrayList>, Appending ArrayList with new values based on the Key
    text
    copied!<p>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.</p> <pre><code>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............ </code></pre> <p>For my end result, when I print out the results of myClass, I need to see something like: {blargh=[blarghvalue, anotherstring], blargh2=uglystring}</p> <p>I need to be able to add to this, and remove values as well.</p> <p>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:</p> <pre><code>public void addingMethod(String key, String value) { hashMap.put(key, value); </code></pre> <p>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. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload