Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当某个枚举实例的参数只有一个值时,如何获取该实例?_Java_Enums - Fatal编程技术网

Java 当某个枚举实例的参数只有一个值时,如何获取该实例?

Java 当某个枚举实例的参数只有一个值时,如何获取该实例?,java,enums,Java,Enums,请在下面找到我的枚举类: public enum CountryCode //implements IEnum { AD("Andorra", "AND", "Europe", "South West Europe"), AE("United Arab Emirates", "ARE", "Asia", "South West Asia"), AF("Afghanistan", "AFG", "Asia", "South Asia"), // and so one till Z // attr

请在下面找到我的枚举类:

public enum CountryCode //implements IEnum
{
AD("Andorra", "AND", "Europe", "South West Europe"),
AE("United Arab Emirates", "ARE", "Asia", "South West Asia"),
AF("Afghanistan", "AFG", "Asia", "South Asia"),
// and so one till Z

// attributes defined
private final String label;
private final String isoCode3;
private final String continent;
private final String region;

// countructor defined
private CountryCode(String label, String isoCode3, String continent, String region)
{
    this.label = label;
    this.isoCode3 = isoCode3;
    this.continent = continent;
    this.region = region;

}   // END CountryCode

} // END enum CountryCode

现在,如果只给我国家名称,即标签,那么如何获得其对应的enum实例?因此,如果给定“安道尔”,我想要
AD

首先让我们看看简单但无效的解决方案:

public static CountryCode  getCountryCodeByLabel(String label)
    for(CountryCode  c : CountryCode.values()) {
        if(label.equals(c.label())) {
            return c;
        }
    }
    return null;
}
要改进此解决方案,您可以在枚举内创建
Map
,并将其
put()
调用到构造函数中。在这种情况下,
getCountryCodeByLabel(字符串标签)
将如下所示:

public static CountryCode  getCountryCodeByLabel(String label)
    return labels.get(label);
}
显然,您可以创建几个这样的映射:每个映射都可以使用不同的字段作为键

编辑:

请看我的文章,我在文章中详细解释了这个问题


你也可以阅读我的博客:

用你提供的代码是不可能的。您需要有一个方法从枚举的值中获取
标签
。或枚举中从
标签返回代码的方法。我在下面展示第一种方式

public enum CountryCode {
  AD("Andorra", "AND", "Europe", "South West Europe"),
  AE("United Arab Emirates", "ARE", "Asia", "South West Asia"),
  AF("Afghanistan", "AFG", "Asia", "South Asia");
  // and so one till Z

  // attributes defined
  private final String label;
  private final String isoCode3;
  private final String continent;
  private final String region;

  // countructor defined
  private CountryCode(String label, String isoCode3, String continent, String region) {
    this.label = label;
    this.isoCode3 = isoCode3;
    this.continent = continent;
    this.region = region;
  } // END CountryCode
  // ADDED THIS 
  public String getLabel() {
    return this.label;
  }// END ADDED THIS :)
} // END enum CountryCode
在循环这些值并找到与标签匹配的值之后

for (CountryCode cc : CountryCode.values()) {
  cc.getLabel().equals(yourCode) {
    // cc is the correct CountryCode
  }
}

我会在地图中缓存关系,其中键是标签和相应国家代码的值。这样,您只需在CountryCode实例上迭代一次,而不是每次调用
getByLabel()
方法

公共枚举国家代码{
AD(“安道尔”、“欧洲”、“西南欧”),
AE(“阿拉伯联合酋长国”、“ARE”、“亚洲”、“西南亚”),
阿富汗(“阿富汗”、“阿富汗政府”、“亚洲”、“南亚”);
私人静态最终地图由_标签;
静止的{
Map byLabel=newhashmap();
对于(CountryCode CountryCode:values()){
byLabel.put(countryCode.label,countryCode);
}
BY_LABEL=Collections.unmodifiableMap(byLabel);
}
私有最终字符串标签;
私有最终字符串等码3;
非洲大陆的私人最终字符串;
私有最终字符串区域;
专用国家代码(字符串标签、字符串等码3、字符串洲、字符串地区){
this.label=标签;
这一点。等代码3=等代码3;
这个大陆=大陆;
这个区域=区域;
}
公共静态国家代码getByLabel(字符串标签){
按标签返回。获取(标签);
}
}
然后,简单地呼叫:

CountryCode CountryCode=CountryCode.getByLabel(“安道尔”);

如果安道尔,你想要AD?@GI Joe,是的,确实是一张
地图
,由
枚举
和它的国家名组成(该
枚举
已经有了国家名)?似乎是为了有一个高效查找的机会。如果您不必复制任何内容并使用枚举中已有的内容,那就太好了。您提到的第一种方法我已经尝试过了,但它会引发异常!对于第二个代码,它的语法正确吗?我正在考虑EnumMap,但我不知道如何在这种情况下实现它。如果您将
enum
初始化为
AD(“and”,“Europe”,“South West Europe”)
etc,并且有一个
EnumMap
,其中包含所有
enum的
,以及它们相应的国家名称?谢谢,现在如果我有一个字符串形式的值,比如“AD”,那么如何将其转换或解析为CountryCode类型呢?@GIJoe我真的不明白你的意思,但是你可以根据需要有很多内部常量映射(例如,
BY_ISO_code\u 3
BY_containment
)。因此,您只需创建相应的
getByIsoCode3()
getbycontainment()
静态方法。@Amir只需使用本机的
CountryCode.valueOf(“AD”)
method.是的,它很简单!我已经用这种方法尝试过了,但它不起作用,并且引发了exception=list!java.lang.NullPointerException您添加了一个方法来获取标签并在什么时候获取异常??