0

Okay, so I'm fairly new to programming so apologies if this problem is really simple, I've created an ArrayList inside my Hash Map so that I can add more than one value into my hash map.

My code looks fine (to me) but theres an error on this line "mymap.add(module, new ArrayList>());" saying ( or [ expected.

Wasted way too much time trying to figure this out so thought I'd post here. Thanks in advance for any help. Also: yes I need to do it this way and no I can't use guava MultiMap.

public class hashArray {
HashMap<String, ArrayList<Integer>> mymap = new HashMap<String, ArrayList<Integer>>();
public hashArray() {}
public void addEntryHR( String module, Integer result ) {
mymap.add(module, new ArrayList<Integer>>());
1
  • 1
    put is not adding my entries into the arraylist? Commented Dec 13, 2017 at 12:25

3 Answers 3

4

There is a typo and a bug in your line:

//         The typo is right here    v
mymap.add(mod, new ArrayList<Integer>>());

Remove one of the > and change add to put:

mymap.put(mod, new ArrayList<Integer>());

The error you get, is about the typo. Fixing that typo will give you an error about add to be an unknown method.

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

Comments

2

You need to first get the list out from map object like below-

ArrayList<Integer> list = mymap.get(mod);

if (list==null){
    //then create new ArrayList object and assign to list
    list=new ArrayList<Integer>();
 }
  list.add(number);  //adding new number to the list
  mymap.put(mod,list); //storing in map

Comments

2

The problems

  • You create a new ArrayList in new ArrayList<Integer>>() (with contains a syntax error as well - a > too many), but never add number to it.
  • You are calling add on a HashMap, which doesn't have this method. For maps, it is called put.

Proposed solution

Please see the code below

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

public void addEntryHR(String mod, Integer number) {
    List<Integer> numbers = new ArrayList<>();
    numbers.add(number);

    myMap.put(mod, numbers);
}

Other remarks

Use interfaces
It is advised to use the interfaces of the collections rather than the implementations. This means Map<String, String> myMap instead of HashMap<String, String> myMap and List<Integer> instead of ArrayList<Integer>.
Why do this? Because this allows you to be flexible in the collections you use and reduce maintenance effort. Say you change your mind and want to use LinkedList instead of ArrayList, then you just have to change one new ArrayList<>() to new LinkedList<>() instead of all the ArrayList variables used throughout the code.

2 Comments

Thank you I believe this is the solution I was looking for, however one problem, when I change my code to this I get the following error: "diamond operator is not supported in -source 1.6 (use-source 7 or higher to enable diamond operator)" - so I edited the project properties to be using source 7 and 90% of my code now appears non-responsive. Any ideas? Thanks.
Is it linked with the right version of JDK? What IDE are you using? I can't help much without any proper details. If the problem still persists, I suggest raising a new question on how to use Java 7+ with your IDE. As a workaround, you can use new ArrayList<Integer>() and new HashMap<String, List<Integer>>() until you resolved the issue.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.