Creating an EnumMap in a generic

M

Mick

Hello, I'm wondering if anyone has ever tried to create an EnumMap
object within a generic, such that the EnumMap keyed based off the
class parameter for that generic. Here is a sample of code that I
thought would logically work for this problem:

START_SNIPPET

public class GenericClass<P extends Enum> {

private Map<P,String> properties;

public GenericClass() {
properties = new EnumMap<P,String>( new HashMap<P,String>() );
// compile error
}

END_SNIPPET

Eclipse points out the following compile error directly on the first
'P' in the marked line of code: "Bound mismatch: The type P is not a
valid substitute for the bounded parameter <K extends Enum<K>> of the
type EnumMap<K,V>"

Does anyone know what I am doing wrong, or perhaps a better way to
accomplish what I am looking for? I realize I could just use a HashMap
for this situation, but I'm curious to know if what I'm attempting is
possible. It seems like it should be possible to use an EnumMap in
this situation.
 
T

Thomas Fritsch

Mick said:
START_SNIPPET import java.util.*;

public class GenericClass<P extends Enum> {
public class GenericClass said:
private Map<P,String> properties;

public GenericClass() {
properties = new EnumMap<P,String>( new HashMap<P,String>() );
// compile error
} }

END_SNIPPET

Eclipse points out the following compile error directly on the first
'P' in the marked line of code: "Bound mismatch: The type P is not a
valid substitute for the bounded parameter <K extends Enum<K>> of the
type EnumMap<K,V>"
In your first line you have declared P to be
<P extends Enum>
Hence the compiler complains. To make it happy you have to declare
 
M

Mick

Thomas said:
In your first line you have declared P to be
<P extends Enum>
Hence the compiler complains. To make it happy you have to declare
<P extends Enum<P>>


That's exactly what I was looking for; thank you.
 
M

Mick

Apparently this solution does not work. You cannot create a new
EnumMap based off an empty HashMap regardless of the types used.
 
R

Roland de Ruiter

Apparently this solution does not work. You cannot create a new
EnumMap based off an empty HashMap regardless of the types used.

You could pass the class denoting the key type as parameter to your
constructor (like EnumMap<K extends Enum<K>, V> has a contructor with
Class<K> as parameter):


public class GenericClass<P extends Enum<P>>
{
private Map<P,String> properties;
public GenericClass(Class<P> keyType)
{
properties = new EnumMap<P,String>( keyType );
}
}


Usage example:

GenericClass<Thread.State> foo =
new GenericClass<Thread.State>(Thread.State.class);
 
M

Mick

Roland said:
You could pass the class denoting the key type as parameter to your
constructor (like EnumMap<K extends Enum<K>, V> has a contructor with
Class<K> as parameter):

That makes a lot of sense, and it made me realize why EnumMap has that
seemingly silly constructor in the first place. I had moved on happily
with a plain-old HashMap, but I'll make a note to go revisit that code.

Thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top