<分区>
我想在 Java 中将整数映射到枚举。整数需要是键,枚举是值。我见过的所有示例都以枚举作为值,以整数作为键。我有:
enum CardStatus {
UNKNOWN, // 0 should map to this
PENDING, // 1 should map to this
ACTIVE // 2 should map to this
public CardStatus getCardStatus(int cardStatus) {
// e.g. cardStatus of 1 would return PENDING
}
}
如何调用 getCardStatus()
并取回 CardStatus
枚举?例如。 getCardStatus(2)
将返回 ACTIVE