Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap with enums

 
Bharath Kumar Alapati
Greenhorn
Posts: 11
Chrome Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i'm working with jdk1.6.
i'm having 2 enums as below:
public enum c1 {
a(1,2,3),
b(1,2,3),
c(1,2,3);
...
}

public enum c2{
A,
B,
C;
...
}


now from test class i want to create a hashmap with key from enum c1, value from enum c2.

Please help me:
my peice of code is:
public class test {
HashMap<c1, c2> mapping = new HashMap<c1, c2>();
mapping.put(c1.a, c2.A);
mapping.put(c1.b, c2.B);
mapping.put(c1.c, c2.C);
}

this is not working...
any ideas how to make this work...
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are trying to put things in the map outside a method declaration or initializer block. Move your calls to mapping.put() into a method:


[ August 14, 2008: Message edited by: Mark Vedder ]
 
Rob Spoor
Sheriff
Pie
Posts: 20531
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may want to change from HashMap to EnumMap. That class has been built especially for enumerations. It uses the enum ordinals for lookups instead of the hash codes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic