反向查找每个键/常量具有多个值的Java枚举?
使用像这样的枚举,其中每个键都有多个值反向查找每个键/常量具有多个值的Java枚举?,java,enums,lookup,reverse-lookup,Java,Enums,Lookup,Reverse Lookup,使用像这样的枚举,其中每个键都有多个值 ABBR1("long text 1", "another description 1", "yet another one 1"), ABBR2("long text 2", "another description 2", "yet another one 2"), //and so on... 如何通过调用类似于get缩写(descriptionontext)的方法反向查找缩写(常量) 我想,我基本上是在寻找一个如上所述的实现,但不同的是,每个
ABBR1("long text 1", "another description 1", "yet another one 1"),
ABBR2("long text 2", "another description 2", "yet another one 2"),
//and so on...
如何通过调用类似于get缩写(descriptionontext)
的方法反向查找缩写(常量)
我想,我基本上是在寻找一个如上所述的实现,但不同的是,每个枚举键(常量)都有几个值,我希望它能与get缩写(“长文本1”)
以及get缩写(“另一个2”)
是否有一种简单的方法可以循环每个枚举(即
ABBRn
)的值字段,以填充一个巨大的映射,或者是否有更好的解决方案?每个枚举都有一个方法values(),因此您可以这样做:
for(YourEnum type : values()){
if(/*is Equal to Your descriptionText*/){
return type;
}
}
throw new IllegalArgumentException("No such BailiffPaymentStatus:"+dbRepresentation);
每个枚举都有一个方法values(),因此您可以这样做:
for(YourEnum type : values()){
if(/*is Equal to Your descriptionText*/){
return type;
}
}
throw new IllegalArgumentException("No such BailiffPaymentStatus:"+dbRepresentation);
这取决于枚举成员构造函数在静态初始值设定项之前运行这一事实。然后,初始值设定项缓存成员及其长格式
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public enum Abbreviation {
ABBR1("long text 1", "another description 1", "yet another one 1"),
ABBR2("long text 2", "another description 2", "yet another one 2");
private static final Map<String, Abbreviation> ABBREVIATIONS = new HashMap<>();
private String[] longForms;
private Abbreviation(String... longForms) {
this.longForms = longForms;
}
public String toString () {
return Arrays.toString(longForms);
}
static {
for(Abbreviation abbr : values()) {
for(String longForm : abbr.longForms) {
ABBREVIATIONS.put(longForm, abbr);
}
}
}
public static Abbreviation of(String longForm) {
Abbreviation abbreviation = ABBREVIATIONS.get(longForm);
if(abbreviation == null) throw new IllegalArgumentException(longForm + " cannot be abbreviated");
return abbreviation;
}
public static void main(String[] args) {
Abbreviation a = Abbreviation.of("yet another one 2");
System.out.println(a == Abbreviation.ABBR2); //true
}
}
导入java.util.array;
导入java.util.HashMap;
导入java.util.Map;
公共枚举缩写{
缩写1(“长文本1”、“另一描述1”、“另一描述1”),
缩写2(“长文本2”,“另一描述2”,“另一描述2”);
私有静态最终映射缩写=new HashMap();
私有字符串[]长格式;
私人缩写(字符串…长格式){
this.longForms=longForms;
}
公共字符串toString(){
返回数组.toString(longForms);
}
静止的{
for(缩写缩写为abbr:values()){
for(字符串长格式:缩写为longForms){
缩写。put(长格式,缩写);
}
}
}
公共静态缩写(字符串长格式){
缩写=缩写.get(长格式);
如果(缩写==null)抛出新的IllegalArgumentException(longForm+“不能缩写”);
返回缩写;
}
公共静态void main(字符串[]args){
缩写a=的缩写(“另一个2”);
System.out.println(a==缩写.ABBR2);//true
}
}
这取决于枚举成员构造函数在静态初始值设定项之前运行的事实。然后,初始值设定项缓存成员及其长格式
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public enum Abbreviation {
ABBR1("long text 1", "another description 1", "yet another one 1"),
ABBR2("long text 2", "another description 2", "yet another one 2");
private static final Map<String, Abbreviation> ABBREVIATIONS = new HashMap<>();
private String[] longForms;
private Abbreviation(String... longForms) {
this.longForms = longForms;
}
public String toString () {
return Arrays.toString(longForms);
}
static {
for(Abbreviation abbr : values()) {
for(String longForm : abbr.longForms) {
ABBREVIATIONS.put(longForm, abbr);
}
}
}
public static Abbreviation of(String longForm) {
Abbreviation abbreviation = ABBREVIATIONS.get(longForm);
if(abbreviation == null) throw new IllegalArgumentException(longForm + " cannot be abbreviated");
return abbreviation;
}
public static void main(String[] args) {
Abbreviation a = Abbreviation.of("yet another one 2");
System.out.println(a == Abbreviation.ABBR2); //true
}
}
导入java.util.array;
导入java.util.HashMap;
导入java.util.Map;
公共枚举缩写{
缩写1(“长文本1”、“另一描述1”、“另一描述1”),
缩写2(“长文本2”,“另一描述2”,“另一描述2”);
私有静态最终映射缩写=new HashMap();
私有字符串[]长格式;
私人缩写(字符串…长格式){
this.longForms=longForms;
}
公共字符串toString(){
返回数组.toString(longForms);
}
静止的{
for(缩写缩写为abbr:values()){
for(字符串长格式:缩写为longForms){
缩写。put(长格式,缩写);
}
}
}
公共静态缩写(字符串长格式){
缩写=缩写.get(长格式);
如果(缩写==null)抛出新的IllegalArgumentException(longForm+“不能缩写”);
返回缩写;
}
公共静态void main(字符串[]args){
缩写a=的缩写(“另一个2”);
System.out.println(a==缩写.ABBR2);//true
}
}
我认为:c.p.u1的解决方案是正确的。但是,有一种更直接的方法可以在使用此问题的解决方案时填充HashMap
-
私有静态最终地图缩写地图; 私人缩写(字符串…长格式){ this.longForms=longForms;//可选 地图缩略语(此为长格式) } 私有静态void映射缩写(最终状态状态、字符串…长格式){ if(null==缩写映射){ abbreviationMap=新的HashMap(20); } for(字符串缩写:longForms){ abbrev,state; } }
另外,toString()函数实际上不需要私有的
longForms
string数组,因为所有的值都与映射一起保存。我认为:c.p.u1的解决方案是正确的。但是,有一种更直接的方法可以在使用此问题的解决方案时填充HashMap
-
私有静态最终地图缩写地图; 私人缩写(字符串…长格式){ this.longForms=longForms;//可选 地图缩略语(此为长格式) } 私有静态void映射缩写(最终状态状态、字符串…长格式){ if(null==缩写映射){ abbreviationMap=新的HashMap(20); } for(字符串缩写:longForms){ abbrev,state; } }
此外,toString()函数实际上不需要私有的
longForms
string数组,因为所有的值都与映射一起保存。您知道定义了哪个Java类values()
方法吗?它是java.lang Enum
中的一个吗?如果是这样的话,那个类必须描述一个枚举常量及其值-这会引起一个问题,枚举“类”是在哪里定义的…?是的,它是在枚举类本身中定义的。如果要反编译枚举,您会注意到is或多或少是一个非常简单的类@Christian中没有values()
方法,它是实际枚举类型的合成方法。@kapep:我假设该类型本身是用Java实现的?如果是的话,有没有办法查看源代码?只是好奇…@Christian它是编译器生成的,所以我认为你找不到真正的源文件。java规范应该包含一些信息