• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

HashMap with enums

 
Greenhorn
Posts: 11
MyEclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 22862
132
Eclipse IDE Spring TypeScript Quarkus Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic