Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Creating an EnumMap in a generic

Thread view: 
Mick - 23 Jun 2006 11:11 GMT
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.
Thomas Fritsch - 23 Jun 2006 12:29 GMT
> START_SNIPPET
  import java.util.*;

> public class GenericClass<P extends Enum> {
  public class GenericClass<P extends Enum<P>> {

>   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
    <P extends Enum<P>>

> Does anyone know what I am doing wrong, ...

Signature

Thomas

Mick - 23 Jun 2006 13:15 GMT
> 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.
Mick - 23 Jun 2006 22:58 GMT
Apparently this solution does not work.  You cannot create a new
EnumMap based off an empty HashMap regardless of the types used.
Roland de Ruiter - 24 Jun 2006 12:16 GMT
> 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);
Signature

Regards,

Roland

Mick - 25 Jun 2006 02:46 GMT
> 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):

> --
> Regards,
>
> Roland

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!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.