Previously, I had my LegNo enums defined simply as:
以前,我将LegNo枚举定义为:
NO_LEG, LEG_ONE, LEG_TWO
and by calling return LegNo.values()[i];
, I was able to get the value associated with each enum.
并通过调用return LegNo.values()[i];,我能够获得与每个枚举相关联的值。
But now I've decided I want the LegNo
enum NO_LEG
to be the int -1 instead of 0 so I decided to use a private constructor to initialise and set its int value
但是现在我已经决定我希望LegNo枚举NO_LEG为int -1而不是0所以我决定使用私有构造函数初始化并设置其int值
NO_LEG(-1), LEG_ONE(1), LEG_TWO(2);
private LegNo(final int leg) { legNo = leg; }
the only thing now is that because I'm doing it this way the values()
method will not work for the NO_LEG
enum. How do I get the enum associated with the int? Is there any efficient way of doing this other than using a case switch statement or an if-elseif-elseif
现在唯一的事情是,因为我这样做,所以values()方法不适用于NO_LEG枚举。如何获得与int关联的枚举?除了使用case switch语句或if-elseif-elseif之外,还有其他有效的方法吗?
I can see a lot of SO questions to do with getting the int value from the enum, but I'm after the reverse.
从enum中获取int值我可以看到很多SO问题,但是我正在反过来。
8 个解决方案
#1
119
You'll have to maintain a mapping inside the enum.
你必须在枚举中维护一个映射。
public enum LegNo {
NO_LEG(-1), LEG_ONE(1), LEG_TWO(2);
private int legNo;
private static Map<Integer, LegNo> map = new HashMap<Integer, LegNo>();
static {
for (LegNo legEnum : LegNo.values()) {
map.put(legEnum.legNo, legEnum);
}
}
private LegNo(final int leg) { legNo = leg; }
public static LegNo valueOf(int legNo) {
return map.get(legNo);
}
}
The static block will be invoked only once, so there is practically no performance issue here.
静态块只会被调用一次,因此这里几乎没有性能问题。
EDIT: Renamed the method to valueOf
as it is more inline with other Java classes.
编辑:将方法重命名为valueOf,因为它更符合其他Java类。
#2
19
You could also include a static method in the enum that iterates through all members and returns the correct one.
您还可以在枚举中包含一个静态方法,该方法遍历所有成员并返回正确的成员。
public enum LegNo {
NO_LEG(-1),
LEG_ONE(1),
LEG_TWO(2);
private int legIndex;
private LegNo(int legIndex) { this.legIndex = legIndex; }
public static LegNo getLeg(int legIndex) {
for (LegNo l : LegNo.values()) {
if (l.legIndex == legIndex) return l;
}
throw new IllegalArgumentException("Leg not found. Amputated?");
}
}
Now, if you want to get an Enum value by the integer, you just use:
现在,如果您想通过整数获得Enum值,您只需使用:
int myLegIndex = 1; //expected : LEG_ONE
LegNo myLeg = LegNo.getLeg(myLegIndex);
#3
11
adarshr's answer adapted to Java 8:
adarshr的答案适用于Java 8:
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toMap;
import java.util.Map;
public enum LegNo {
NO_LEG(-1), LEG_ONE(1), LEG_TWO(2);
private final int legNo;
private final static Map<Integer, LegNo> map =
stream(LegNo.values()).collect(toMap(leg -> leg.legNo, leg -> leg));
private LegNo(final int leg) {
legNo = leg;
}
public static LegNo valueOf(int legNo) {
return map.get(legNo);
}
}
#4
5
You can also access Enum value corresponding to given integer value simply by calling values() method on enum LegNo. It returns field of LegNo enums: LegNo.values[0]; //returns LEG_NO LegNo.values[1]; //returns LEG_ONE LegNo.values[2]; //returns LEG_TWO
您还可以通过调用枚举LegNo上的values()方法来访问与给定整数值对应的Enum值。它返回LegNo枚举的字段:LegNo.values [0]; //返回LEG_NO LegNo.values [1]; //返回LEG_ONE LegNo.values [2]; //返回LEG_TWO
Not precisely the thing he was looking for, but pretty close though and very simple indeed. (Although the subject is dead it might be useful for someone else.)
不是他正在寻找的东西,但非常接近但确实非常简单。 (虽然主题已经死了,但对其他人来说可能有用。)
#5
3
Java 8 way with default value:
Java 8方式的默认值:
public enum LegNo {
NO_LEG(-1), LEG_ONE(1), LEG_TWO(2);
private final int legNo;
LegNo(int legNo) {
this.legNo = legNo;
}
public static LegNo find(int legNo, Supplier<? extends LegNo> byDef) {
return Arrays.asList(LegNo.values()).stream()
.filter(e -> e.legNo == legNo).findFirst().orElseGet(byDef);
}
}
to call:
致电:
LegNo res = LegNo.find(0, () -> LegNo.NO_LEG);
or with Exception:
或者有例外:
LegNo res = LegNo.find(0, () -> {
throw new RuntimeException("No found");
});
#6
1
Since your enum only contains 3 elements, the fastest way will be to just use a series of if else
, like you suggested.
由于你的枚举只包含3个元素,最快的方法就是使用一系列if else,就像你建议的那样。
edit: the answer that adarshr provided is better suited for general cases, where there are many enum values, but I think it is an overkill for your problem.
编辑:adarshr提供的答案更适合一般情况,其中有许多枚举值,但我认为这对你的问题来说太过分了。
#7
0
public enum LegNo {
NO_LEG(-1), LEG_ONE(1), LEG_TWO(2);
private int legNo;
private LegNo(int leg) { legNo = leg; }
public static LegNo valueOf(int legNo) {
for (LegNo leg : LegNo.values()) {
if (leg.legNo == legNo) return leg;
}
}
}
assert LegNo.valueOf(2) == LegNo.LEG_TWO
assert LegNo.valueOf(3) == null
#8
-4
public enum body
{
NO_LEG = 0, // bit 1
LEG_ONE = 2, // bit 2
LEG_TWO = 4, // bit 3
NO_ARM = 8, // bit 4
ARM_ONE = 16, // bit 5
ARM_TWO = 32 // bit 6
}
use it like this (int)body.LEG_ONE ; /// which returns 2
像这样使用它(int)body.LEG_ONE; ///返回2
this is also very fast, and easy readable since it is done on compile time.
这也非常快,并且易于阅读,因为它是在编译时完成的。
#1
119
You'll have to maintain a mapping inside the enum.
你必须在枚举中维护一个映射。
public enum LegNo {
NO_LEG(-1), LEG_ONE(1), LEG_TWO(2);
private int legNo;
private static Map<Integer, LegNo> map = new HashMap<Integer, LegNo>();
static {
for (LegNo legEnum : LegNo.values()) {
map.put(legEnum.legNo, legEnum);
}
}
private LegNo(final int leg) { legNo = leg; }
public static LegNo valueOf(int legNo) {
return map.get(legNo);
}
}
The static block will be invoked only once, so there is practically no performance issue here.
静态块只会被调用一次,因此这里几乎没有性能问题。
EDIT: Renamed the method to valueOf
as it is more inline with other Java classes.
编辑:将方法重命名为valueOf,因为它更符合其他Java类。
#2
19
You could also include a static method in the enum that iterates through all members and returns the correct one.
您还可以在枚举中包含一个静态方法,该方法遍历所有成员并返回正确的成员。
public enum LegNo {
NO_LEG(-1),
LEG_ONE(1),
LEG_TWO(2);
private int legIndex;
private LegNo(int legIndex) { this.legIndex = legIndex; }
public static LegNo getLeg(int legIndex) {
for (LegNo l : LegNo.values()) {
if (l.legIndex == legIndex) return l;
}
throw new IllegalArgumentException("Leg not found. Amputated?");
}
}
Now, if you want to get an Enum value by the integer, you just use:
现在,如果您想通过整数获得Enum值,您只需使用:
int myLegIndex = 1; //expected : LEG_ONE
LegNo myLeg = LegNo.getLeg(myLegIndex);
#3
11
adarshr's answer adapted to Java 8:
adarshr的答案适用于Java 8:
import static java.util.Arrays.stream;
import static java.util.stream.Collectors.toMap;
import java.util.Map;
public enum LegNo {
NO_LEG(-1), LEG_ONE(1), LEG_TWO(2);
private final int legNo;
private final static Map<Integer, LegNo> map =
stream(LegNo.values()).collect(toMap(leg -> leg.legNo, leg -> leg));
private LegNo(final int leg) {
legNo = leg;
}
public static LegNo valueOf(int legNo) {
return map.get(legNo);
}
}
#4
5
You can also access Enum value corresponding to given integer value simply by calling values() method on enum LegNo. It returns field of LegNo enums: LegNo.values[0]; //returns LEG_NO LegNo.values[1]; //returns LEG_ONE LegNo.values[2]; //returns LEG_TWO
您还可以通过调用枚举LegNo上的values()方法来访问与给定整数值对应的Enum值。它返回LegNo枚举的字段:LegNo.values [0]; //返回LEG_NO LegNo.values [1]; //返回LEG_ONE LegNo.values [2]; //返回LEG_TWO
Not precisely the thing he was looking for, but pretty close though and very simple indeed. (Although the subject is dead it might be useful for someone else.)
不是他正在寻找的东西,但非常接近但确实非常简单。 (虽然主题已经死了,但对其他人来说可能有用。)
#5
3
Java 8 way with default value:
Java 8方式的默认值:
public enum LegNo {
NO_LEG(-1), LEG_ONE(1), LEG_TWO(2);
private final int legNo;
LegNo(int legNo) {
this.legNo = legNo;
}
public static LegNo find(int legNo, Supplier<? extends LegNo> byDef) {
return Arrays.asList(LegNo.values()).stream()
.filter(e -> e.legNo == legNo).findFirst().orElseGet(byDef);
}
}
to call:
致电:
LegNo res = LegNo.find(0, () -> LegNo.NO_LEG);
or with Exception:
或者有例外:
LegNo res = LegNo.find(0, () -> {
throw new RuntimeException("No found");
});
#6
1
Since your enum only contains 3 elements, the fastest way will be to just use a series of if else
, like you suggested.
由于你的枚举只包含3个元素,最快的方法就是使用一系列if else,就像你建议的那样。
edit: the answer that adarshr provided is better suited for general cases, where there are many enum values, but I think it is an overkill for your problem.
编辑:adarshr提供的答案更适合一般情况,其中有许多枚举值,但我认为这对你的问题来说太过分了。
#7
0
public enum LegNo {
NO_LEG(-1), LEG_ONE(1), LEG_TWO(2);
private int legNo;
private LegNo(int leg) { legNo = leg; }
public static LegNo valueOf(int legNo) {
for (LegNo leg : LegNo.values()) {
if (leg.legNo == legNo) return leg;
}
}
}
assert LegNo.valueOf(2) == LegNo.LEG_TWO
assert LegNo.valueOf(3) == null
#8
-4
public enum body
{
NO_LEG = 0, // bit 1
LEG_ONE = 2, // bit 2
LEG_TWO = 4, // bit 3
NO_ARM = 8, // bit 4
ARM_ONE = 16, // bit 5
ARM_TWO = 32 // bit 6
}
use it like this (int)body.LEG_ONE ; /// which returns 2
像这样使用它(int)body.LEG_ONE; ///返回2
this is also very fast, and easy readable since it is done on compile time.
这也非常快,并且易于阅读,因为它是在编译时完成的。