简体   繁体   中英

Mapping inside an enum

I have an enum declared like this

 public enum KDErrors {

  KDR_280(280, "Blue"),
  KDR_281(281, "Red"),
  KDR_282(282, "Green"),
  KDR_284(284, "Yellow");

  private final int code;
  private final String color;

  private KDErrors(int code, String color) {
    this.code = code;
    this.color = color;
  }

  public static String getColorByCode(int colorCode) {

return ???
  }
}

I want to get the value by passing the code, how can I make it work?

Change code to:

public enum KDErrors {

    KDR_280(280, "Blue"), KDR_281(281, "Red"), KDR_282(282, "Green"), KDR_284(284, "Yellow");

    private final int code;

    private final String color;

    private KDErrors(int code, String color) {
        this.code = code;
        this.color = color;
    }

    public int getCode() {
        return code;
    }

    public String getColor() {
        return color;
    }

    public static String getColorByCode(int colorCode) {
        for (KDErrors error : KDErrors.values()) {
            if (error.getCode() == colorCode)
                return error.getColor();
        }
        return null;
    }

}

You could use a reverse lookup map of either you enum or code

Below is an example using reverse look up for enum based on code :

public enum KDErrors {

  private static Map<Integer, KDErrors> reverseLookUp = new HashMap<>();

  static{
    for (KDErrors error : KDErrors.values()) {
      reverseLookUp.put(error.code, error);
    }
  }
  //you method would look like
  public static String getColorByCode(int colorCode) {
    if(reverseLookUp.get(colorCode) == null)
      return null;
    else 
      return reverseLookUp.get(colorCode).color;
  }
}

You have a couple of straightforward options available to you. One is to use a Map<Integer, String> and another is to to a linear search.

Using a map is probably much more efficient, but will take up more space. This is probably not an issue for a small number of enums.

public enum KDErrors {

  KDR_280(280, "Blue"),
  KDR_281(281, "Red"),
  KDR_282(282, "Green"),
  KDR_284(284, "Yellow"),

  private static Map<Integer, String> codeMap = new HashMap<>();

  private final int code;
  private final String color;

  private KDErrors(int code, String color) {
    this.code = code;
    this.color = color;
    codeMap.put(code, color);
  }

  public static String getColorByCode(int colorCode) {
   return codeMap(colorCode);
  }
}

Doing a linear search prevents you from having to allocate additional structures, but it will be a little slower for the average case than a lookup in a HashMap :

public enum KDErrors {

  KDR_280(280, "Blue"),
  KDR_281(281, "Red"),
  KDR_282(282, "Green"),
  KDR_284(284, "Yellow"),

  private final int code;
  private final String color;

  private KDErrors(int code, String color) {
    this.code = code;
    this.color = color;
  }

  public static String getColorByCode(int colorCode) {
   for(KDErrors err : KDErrors.values()) {
       if(err.getCode() == colorCode)
           return error.getColor();
    }
    return null;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM