wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes HashMap with enums Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashMap with enums" Watch "HashMap with enums" New topic
Author

HashMap with enums

Bharath Kumar Alapati
Greenhorn

Joined: Aug 14, 2008
Posts: 11

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...


Thanks,
~Alapati
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap with enums
 
Similar Threads
hashCode() and equals() - How it is possible
overriding + overloading
overloading problem, pls help!
protected member
Objects available for GC..