0

I've been trying to do this for a little while and it's driving me crazy. I have an EnumMap where I have some enumeration as a key and an arraylist of a self-defined class as the value. So it looks something like this:

private EnumMap<myEnum,List<myObj>> map = 
    new EnumMap<myEnum,List<myObj>>(myEnum.class);

This keeps giving me an error. Not sure exactly what's happening.

EDIT: Yes, myEnum is an enum class. My mistake, I should have mentioned what error is and where it happens. The error occurs when I do the following:

hand.put(myEnum.someEnum, new ArrayList());

The error I get is: - Syntax error on tokens, TypeArgument1 expected instead - Syntax error on token "(", < expected - Syntax error on token "new", delete this token

anujahooja
  • 30
  • 3
  • 6
  • Is `myEnum` the name of your enum class? If not it, it should be (can't tell from your camel-casing). If so, post the error. Is it a compile error? – Melv Mar 17 '11 at 03:26
  • you code seems ok, maybe you should post more info – Marco Aviles Mar 17 '11 at 03:36
  • My mistake, I should have mentioned what error is and where it happens. The error occurs when I do the following: hand.put(myEnum.someEnum, new ArrayList()); The error I get is: - Syntax error on tokens, TypeArgument1 expected instead - Syntax error on token "(", < expected - Syntax error on token "new", delete this token – anujahooja Mar 17 '11 at 03:38
  • 1
    If my answer below doesn't clear it up, you might need to post your entire code block, including the enum definition. It's hard to see what's going wrong from such little context. The error you're getting is fairly odd. – Melv Mar 17 '11 at 03:42

4 Answers4

0

Also, I assume myEnum refers to the class name and not an instance of class MyEnum and same for myObj. We usually start class names with uppercase letters, so this is a little bit confusing.

hand.put(myEnum.someEnum, new ArrayList());

You need to parameterize the ArrayList with ();

0

For:

enum Test {
  TEST0,
  TEST1;
}

You would use the EnumMap like this:

EnumMap<Test, List<Object>> map = new EnumMap<Test, List<Object>>(Test.class);
map.put(Test.TEST0, new ArrayList<Object>());

Hopefully that clears up the usage.

Melv
  • 2,201
  • 16
  • 14
  • Yeah, that's exactly what I'm doing (I added in my edit) and I'm still getting the errors I stated above. It should work according to me as well. – anujahooja Mar 17 '11 at 03:42
0
   hand.put(myEnum.someEnum, new ArrayList());

should be like this:

   map.put(myEnum.someEnum, new ArrayList<myObj>());
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
0

melv's code seems to work:

import java.util.*;
enum Color {
    r,g,b;
}
public class Main{
    public static void main(String[] args) {
        EnumMap<Color, List<Object>> map = new EnumMap<Color, List<Object>>(Color.class);
        map.put(Color.r, new ArrayList<Object>());
        map.get(Color.r).add(new Integer(42));
        System.out.println(map.get(Color.r).get(0));
    }
}
Ray Tayek
  • 9,841
  • 8
  • 50
  • 90