Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why EnumMap constructor needs class argument?

Tags:

java

enums

EnumMap class constructor needs class as the argument. Most of the times K.class passed as the argument. I am still not getting what is the reason for accepting this as argument instead of deducing from K.

Thanks
-- pkc

like image 920
prasadvk Avatar asked Aug 11 '10 14:08

prasadvk


People also ask

Why is EnumMap faster than HashMap?

EnumMap is much faster than HashMap. All keys of each EnumMap instance must be keys of the same enum type. EnumMap doesn't allow inserting null key if we try to insert the null key, it will throw NullPointerException. EnumMap is internally represented as arrays therefore it gives the better performance.

What's the difference between an enum constructor and a regular class constructor?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

What is the use of EnumMap in Java?

The EnumMap class of the Java collections framework provides a map implementation for elements of an enum. In EnumMap , enum elements are used as keys. It implements the Map interface.

Is EnumMap ordered?

EnumMap is an ordered collection and they are maintained in the natural order of their keys(the natural order of keys means the order on which enum constants are declared inside enum type )


5 Answers

Tom's answer is correct, but to address your other point: the reason this information can't just be deduced from the type parameter, K, is due to type erasure.

like image 65
mikej Avatar answered Sep 29 '22 03:09

mikej


The implementations of EnumMap needs metainformation about the enum, in particular the number of values. The Class object provides this information (IMO it would have been better to go for a specific enum descriptor type). If you don't have the Class available, you can always use HashMap at some penalty. I guess you could create a growable/uncommitted EnumMap-like Map.

like image 40
Tom Hawtin - tackline Avatar answered Sep 28 '22 03:09

Tom Hawtin - tackline


The Map thus knows all possible keys. It's called (internally) the keyUniverse. The comments says:

All of the values comprising K. (Cached for performance)

like image 24
Bozho Avatar answered Oct 02 '22 03:10

Bozho


As others point out generics are a compiler feature. The jvm has no real support for generics itself. This means that the generic information cannot be used at runtime.

For the EnumMap<K extends Enum> this means that you get a EnumMap<Enum> at runtime without any information about the K. This limitation of java generics can be worked around by passing the classes of the Generic arguments to a constructor as the class objects still exist at runtime.

like image 26
josefx Avatar answered Sep 30 '22 03:09

josefx


Generics is a compile time feature, however this K class is needed at runtime, something generics won't do in this case.

like image 24
Peter Lawrey Avatar answered Sep 28 '22 03:09

Peter Lawrey