This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA 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: 19684
    
  20

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
 
jQuery in Action, 2nd edition
 
subject: HashMap with enums