Java: Generic method for Enums
Help me understand generics. Say I have two enums as inner classes like so:
public class FoodConstants{
public static enum Vegetable {
POTATO,BROCCOLI,SQUASH,CARROT;
}
public static enum Fruit {
APPLE,MANGO,BANANA,GUAVA;
}
}
Instead of having both enums implement an interface, and have to implement the same method twice, I would like to have a method in the outer class that does something like:
public <e> String getEnumString<Enum<?> e, String s){
for(Enum en: e.values()){
if(en.name().equalsIgnoreCase(s)){
return s;
}
}
return null;
}
However this method does not compile. What I am trying to do is find out if a string value is the name of an enumerated value, in ANY enum, whether it's Vegetable, Fruit, what not.
Regardless of whether this is in fact a redundant method, what is wrong with the one I am trying to (re)write?
Basically I would like to do this:
public class FoodConstants{
public static enum Vegetable {
POTATO,BROCCOLI,SQUASH,CARROT;
}
public static enum Fruit {
APPLE,MANGO,BANANA,GUAVA;
}
public <e> String getEnumString<Enum<?> e, String s){
for(Enum en: e.values()){
if(en.name().equalsIgnoreCase(s)){
return s;
}
}
return null;
}
} //end of code
java generics enums
add a comment |
Help me understand generics. Say I have two enums as inner classes like so:
public class FoodConstants{
public static enum Vegetable {
POTATO,BROCCOLI,SQUASH,CARROT;
}
public static enum Fruit {
APPLE,MANGO,BANANA,GUAVA;
}
}
Instead of having both enums implement an interface, and have to implement the same method twice, I would like to have a method in the outer class that does something like:
public <e> String getEnumString<Enum<?> e, String s){
for(Enum en: e.values()){
if(en.name().equalsIgnoreCase(s)){
return s;
}
}
return null;
}
However this method does not compile. What I am trying to do is find out if a string value is the name of an enumerated value, in ANY enum, whether it's Vegetable, Fruit, what not.
Regardless of whether this is in fact a redundant method, what is wrong with the one I am trying to (re)write?
Basically I would like to do this:
public class FoodConstants{
public static enum Vegetable {
POTATO,BROCCOLI,SQUASH,CARROT;
}
public static enum Fruit {
APPLE,MANGO,BANANA,GUAVA;
}
public <e> String getEnumString<Enum<?> e, String s){
for(Enum en: e.values()){
if(en.name().equalsIgnoreCase(s)){
return s;
}
}
return null;
}
} //end of code
java generics enums
add a comment |
Help me understand generics. Say I have two enums as inner classes like so:
public class FoodConstants{
public static enum Vegetable {
POTATO,BROCCOLI,SQUASH,CARROT;
}
public static enum Fruit {
APPLE,MANGO,BANANA,GUAVA;
}
}
Instead of having both enums implement an interface, and have to implement the same method twice, I would like to have a method in the outer class that does something like:
public <e> String getEnumString<Enum<?> e, String s){
for(Enum en: e.values()){
if(en.name().equalsIgnoreCase(s)){
return s;
}
}
return null;
}
However this method does not compile. What I am trying to do is find out if a string value is the name of an enumerated value, in ANY enum, whether it's Vegetable, Fruit, what not.
Regardless of whether this is in fact a redundant method, what is wrong with the one I am trying to (re)write?
Basically I would like to do this:
public class FoodConstants{
public static enum Vegetable {
POTATO,BROCCOLI,SQUASH,CARROT;
}
public static enum Fruit {
APPLE,MANGO,BANANA,GUAVA;
}
public <e> String getEnumString<Enum<?> e, String s){
for(Enum en: e.values()){
if(en.name().equalsIgnoreCase(s)){
return s;
}
}
return null;
}
} //end of code
java generics enums
Help me understand generics. Say I have two enums as inner classes like so:
public class FoodConstants{
public static enum Vegetable {
POTATO,BROCCOLI,SQUASH,CARROT;
}
public static enum Fruit {
APPLE,MANGO,BANANA,GUAVA;
}
}
Instead of having both enums implement an interface, and have to implement the same method twice, I would like to have a method in the outer class that does something like:
public <e> String getEnumString<Enum<?> e, String s){
for(Enum en: e.values()){
if(en.name().equalsIgnoreCase(s)){
return s;
}
}
return null;
}
However this method does not compile. What I am trying to do is find out if a string value is the name of an enumerated value, in ANY enum, whether it's Vegetable, Fruit, what not.
Regardless of whether this is in fact a redundant method, what is wrong with the one I am trying to (re)write?
Basically I would like to do this:
public class FoodConstants{
public static enum Vegetable {
POTATO,BROCCOLI,SQUASH,CARROT;
}
public static enum Fruit {
APPLE,MANGO,BANANA,GUAVA;
}
public <e> String getEnumString<Enum<?> e, String s){
for(Enum en: e.values()){
if(en.name().equalsIgnoreCase(s)){
return s;
}
}
return null;
}
} //end of code
java generics enums
java generics enums
edited Jun 21 '18 at 8:15


halfer
14.7k759116
14.7k759116
asked May 6 '13 at 20:00


Alexander MillsAlexander Mills
20.2k35163351
20.2k35163351
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
public static <E extends Enum<E>>
String getEnumString(Class<E> clazz, String s){
for(E en : EnumSet.allOf(clazz)){
if(en.name().equalsIgnoreCase(s)){
return en.name();
}
}
return null;
}
The original has a few problems:
- It accepts an instance of the enum instead of the class representing the enum
which your question suggests you want to use. - The type parameter isn't used.
- It returns the input instead of the instance name. Maybe returning the instance would be more useful -- a case-insensitive version of
Enum.valueOf(String)
. - It calls a static method on an instance so you can iterate.
EnumSet
does all the reflective stuff for you.
6
Oh, and clazz.getEnumConstants() would work instead of EnumSet.allOf(clazz). It's probably slightly more efficient, given that EnumSet.allOf is probably implemented in terms of it, but then, maybe not.
– Sebastian Redl
May 6 '13 at 20:12
@SebastianRedl, Odd. I thought that wasn't available in 1.5 but apparently it is.
– Mike Samuel
May 6 '13 at 20:13
6
@SebastianRedl: actually,EnumSet.allOf()
is more efficient than.getEnumConstants()
, because.getEnumConstants()
must make a copy of the internal array of constants (since it returns an array, and there is no way to prevent an array from being modified), whereasEnumSet
can use internal implementation details to directly iterate over the internal array.
– newacct
May 7 '13 at 5:54
using this signature it causes for me a "bad class file", "undeclared type variable: T" errors on compile
– Joe Cabezas
Apr 22 '16 at 22:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16406298%2fjava-generic-method-for-enums%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
public static <E extends Enum<E>>
String getEnumString(Class<E> clazz, String s){
for(E en : EnumSet.allOf(clazz)){
if(en.name().equalsIgnoreCase(s)){
return en.name();
}
}
return null;
}
The original has a few problems:
- It accepts an instance of the enum instead of the class representing the enum
which your question suggests you want to use. - The type parameter isn't used.
- It returns the input instead of the instance name. Maybe returning the instance would be more useful -- a case-insensitive version of
Enum.valueOf(String)
. - It calls a static method on an instance so you can iterate.
EnumSet
does all the reflective stuff for you.
6
Oh, and clazz.getEnumConstants() would work instead of EnumSet.allOf(clazz). It's probably slightly more efficient, given that EnumSet.allOf is probably implemented in terms of it, but then, maybe not.
– Sebastian Redl
May 6 '13 at 20:12
@SebastianRedl, Odd. I thought that wasn't available in 1.5 but apparently it is.
– Mike Samuel
May 6 '13 at 20:13
6
@SebastianRedl: actually,EnumSet.allOf()
is more efficient than.getEnumConstants()
, because.getEnumConstants()
must make a copy of the internal array of constants (since it returns an array, and there is no way to prevent an array from being modified), whereasEnumSet
can use internal implementation details to directly iterate over the internal array.
– newacct
May 7 '13 at 5:54
using this signature it causes for me a "bad class file", "undeclared type variable: T" errors on compile
– Joe Cabezas
Apr 22 '16 at 22:40
add a comment |
public static <E extends Enum<E>>
String getEnumString(Class<E> clazz, String s){
for(E en : EnumSet.allOf(clazz)){
if(en.name().equalsIgnoreCase(s)){
return en.name();
}
}
return null;
}
The original has a few problems:
- It accepts an instance of the enum instead of the class representing the enum
which your question suggests you want to use. - The type parameter isn't used.
- It returns the input instead of the instance name. Maybe returning the instance would be more useful -- a case-insensitive version of
Enum.valueOf(String)
. - It calls a static method on an instance so you can iterate.
EnumSet
does all the reflective stuff for you.
6
Oh, and clazz.getEnumConstants() would work instead of EnumSet.allOf(clazz). It's probably slightly more efficient, given that EnumSet.allOf is probably implemented in terms of it, but then, maybe not.
– Sebastian Redl
May 6 '13 at 20:12
@SebastianRedl, Odd. I thought that wasn't available in 1.5 but apparently it is.
– Mike Samuel
May 6 '13 at 20:13
6
@SebastianRedl: actually,EnumSet.allOf()
is more efficient than.getEnumConstants()
, because.getEnumConstants()
must make a copy of the internal array of constants (since it returns an array, and there is no way to prevent an array from being modified), whereasEnumSet
can use internal implementation details to directly iterate over the internal array.
– newacct
May 7 '13 at 5:54
using this signature it causes for me a "bad class file", "undeclared type variable: T" errors on compile
– Joe Cabezas
Apr 22 '16 at 22:40
add a comment |
public static <E extends Enum<E>>
String getEnumString(Class<E> clazz, String s){
for(E en : EnumSet.allOf(clazz)){
if(en.name().equalsIgnoreCase(s)){
return en.name();
}
}
return null;
}
The original has a few problems:
- It accepts an instance of the enum instead of the class representing the enum
which your question suggests you want to use. - The type parameter isn't used.
- It returns the input instead of the instance name. Maybe returning the instance would be more useful -- a case-insensitive version of
Enum.valueOf(String)
. - It calls a static method on an instance so you can iterate.
EnumSet
does all the reflective stuff for you.
public static <E extends Enum<E>>
String getEnumString(Class<E> clazz, String s){
for(E en : EnumSet.allOf(clazz)){
if(en.name().equalsIgnoreCase(s)){
return en.name();
}
}
return null;
}
The original has a few problems:
- It accepts an instance of the enum instead of the class representing the enum
which your question suggests you want to use. - The type parameter isn't used.
- It returns the input instead of the instance name. Maybe returning the instance would be more useful -- a case-insensitive version of
Enum.valueOf(String)
. - It calls a static method on an instance so you can iterate.
EnumSet
does all the reflective stuff for you.
edited Apr 17 '14 at 15:34
answered May 6 '13 at 20:06
Mike SamuelMike Samuel
94.4k23174215
94.4k23174215
6
Oh, and clazz.getEnumConstants() would work instead of EnumSet.allOf(clazz). It's probably slightly more efficient, given that EnumSet.allOf is probably implemented in terms of it, but then, maybe not.
– Sebastian Redl
May 6 '13 at 20:12
@SebastianRedl, Odd. I thought that wasn't available in 1.5 but apparently it is.
– Mike Samuel
May 6 '13 at 20:13
6
@SebastianRedl: actually,EnumSet.allOf()
is more efficient than.getEnumConstants()
, because.getEnumConstants()
must make a copy of the internal array of constants (since it returns an array, and there is no way to prevent an array from being modified), whereasEnumSet
can use internal implementation details to directly iterate over the internal array.
– newacct
May 7 '13 at 5:54
using this signature it causes for me a "bad class file", "undeclared type variable: T" errors on compile
– Joe Cabezas
Apr 22 '16 at 22:40
add a comment |
6
Oh, and clazz.getEnumConstants() would work instead of EnumSet.allOf(clazz). It's probably slightly more efficient, given that EnumSet.allOf is probably implemented in terms of it, but then, maybe not.
– Sebastian Redl
May 6 '13 at 20:12
@SebastianRedl, Odd. I thought that wasn't available in 1.5 but apparently it is.
– Mike Samuel
May 6 '13 at 20:13
6
@SebastianRedl: actually,EnumSet.allOf()
is more efficient than.getEnumConstants()
, because.getEnumConstants()
must make a copy of the internal array of constants (since it returns an array, and there is no way to prevent an array from being modified), whereasEnumSet
can use internal implementation details to directly iterate over the internal array.
– newacct
May 7 '13 at 5:54
using this signature it causes for me a "bad class file", "undeclared type variable: T" errors on compile
– Joe Cabezas
Apr 22 '16 at 22:40
6
6
Oh, and clazz.getEnumConstants() would work instead of EnumSet.allOf(clazz). It's probably slightly more efficient, given that EnumSet.allOf is probably implemented in terms of it, but then, maybe not.
– Sebastian Redl
May 6 '13 at 20:12
Oh, and clazz.getEnumConstants() would work instead of EnumSet.allOf(clazz). It's probably slightly more efficient, given that EnumSet.allOf is probably implemented in terms of it, but then, maybe not.
– Sebastian Redl
May 6 '13 at 20:12
@SebastianRedl, Odd. I thought that wasn't available in 1.5 but apparently it is.
– Mike Samuel
May 6 '13 at 20:13
@SebastianRedl, Odd. I thought that wasn't available in 1.5 but apparently it is.
– Mike Samuel
May 6 '13 at 20:13
6
6
@SebastianRedl: actually,
EnumSet.allOf()
is more efficient than .getEnumConstants()
, because .getEnumConstants()
must make a copy of the internal array of constants (since it returns an array, and there is no way to prevent an array from being modified), whereas EnumSet
can use internal implementation details to directly iterate over the internal array.– newacct
May 7 '13 at 5:54
@SebastianRedl: actually,
EnumSet.allOf()
is more efficient than .getEnumConstants()
, because .getEnumConstants()
must make a copy of the internal array of constants (since it returns an array, and there is no way to prevent an array from being modified), whereas EnumSet
can use internal implementation details to directly iterate over the internal array.– newacct
May 7 '13 at 5:54
using this signature it causes for me a "bad class file", "undeclared type variable: T" errors on compile
– Joe Cabezas
Apr 22 '16 at 22:40
using this signature it causes for me a "bad class file", "undeclared type variable: T" errors on compile
– Joe Cabezas
Apr 22 '16 at 22:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f16406298%2fjava-generic-method-for-enums%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown