简体   繁体   English

使用地图与枚举

[英]using Map vs Enum


I need to keep a mapping which finds some properties given a String. 我需要保留一个映射,该映射查找给定String的一些属性。 For an example like below, I need to get possible colors for an animal given the description for that animal. 对于下面这样的示例,我需要根据该动物的描述获取该动物的可能颜色。 I have "An animal with four legs" - not "CAT" - and need to find BROWN and GREY. 我有“四脚动物”-不是“ CAT”-并且需要找到布朗和灰色。
Keeping this in an enum makes code more maintainable and easy to read but it has additional cost of traversing all enum values each time I try to find colors. 将其保留在枚举中可使代码更易于维护和易于阅读,但每次我尝试查找颜色时遍历所有枚举值都会产生额外的开销。 And also every time I need to get the colors list, a new array is going to be created. 而且,每当我需要获取颜色列表时,都会创建一个新的数组。
My question is how much these additional costs affect the performance for Animal enum and String set(descriptions I've been given to map) sizes around 20 each? 我的问题是,这些额外费用会分别影响20左右大小的Animal枚举和String设置(已提供映射说明)的性能吗? Also wonder if JVM optimizes these Arrays.asList calls. 还想知道JVM是否优化了这些Arrays.asList调用。
Or any other design solutions you would recommend? 或您会建议的任何其他设计解决方案?
And would really appreciate if you can recommend a source to read more about those performance matters, what affects how much. 如果您能推荐一个来源来阅读更多有关这些性能问题的信息,将对它产生多大的影响,我们将不胜感激。
Thanks! 谢谢!

public enum AnimalsEnum{
    CAT("An animal with four legs"){
        @Override   
        public List<ColorsEnum> getPossibleColors(){
            return Arrays.asList(BROWN, GREY);
        }
    },
    BIRD("An animal with two legs and two wings"){
        @Override   
        public List<ColorsEnum> getPossibleColors(){
            return Arrays.asList(GREY, YELLOW, ORANGE);
        };
    };

    public List<ColorsEnum> getPossibleColors(){
        throw new AbstractMethodError();
    };
}


public enum ColorsEnum{
    ORANGE, BLUE, GREY, BROWN, YELLOW;
}

As far as your doubt "And also every time I need to get the colors list, a new array is going to be created" is concerned, you might try the following: 关于您的疑问“并且每当我需要获取颜色列表时,都会创建一个新的数组”,您可以尝试以下操作:

public enum AnimalsEnum
{
  CAT("An animal with four legs",
      EnumSet.of(ColorsEnum.BROWN, ColorsEnum.GREY)),
  BIRD("An animal with two legs and two wings",
       EnumSet.of(ColorsEnum.GREY, ColorsEnum.YELLOW, ColorsEnum.ORANGE));

  private AnimalsEnum(String              description,
                      EnumSet<ColorsEnum> possible_colors)
  {
    this.description = description;
    this.possible_colors = possible_colors;
  }

  public String getDescription()
  {
    return description;
  }

  public EnumSet<ColorsEnum> getPossibleColors()
  {
    return (possible_colors);
  }

  public static AnimalsEnum getAnimal(String description)
  {
    return (descr_map.get(description));
  }

  private String description;

  private EnumSet<ColorsEnum> possible_colors;

  private static HashMap<String, AnimalsEnum> descr_map;

  static
  {
    descr_map = new HashMap<String, AnimalsEnum>();
    for (AnimalsEnum animal : values())
    {
      descr_map.put(animal.getDescription(), animal);
    }    
  }

} // enum AnimalsEnum

EDIT: 编辑:
Modified the answer adding a static method that returns the animal corresponding to its description. 修改了答案,添加了一个静态方法,该方法返回与其描述相对应的动物。

EDIT2: 编辑2:
If you don't feel like keeping a separate Enum for your animals, you might try this: 如果您不想为动物单独保留Enum ,则可以尝试以下方法:

public class AnimalColors extends HashMap<String, EnumSet<ColorsEnum>>
{
  private AnimalColors()
  {
    put("An animal with four legs", EnumSet.of(ColorsEnum.BROWN, ColorsEnum.GREY));
    put("An animal with two legs and two wings", EnumSet.of(ColorsEnum.GREY, ColorsEnum.YELLOW, ColorsEnum.ORANGE));
  }

  public static AnimalColors get()
  {
    return my_instance;
  }

  private static AnimalColors my_instance = new AnimalColors();

} // class AnimalColors

It's merely opinion based. 这仅仅是基于意见的。 It's up to you. 由你决定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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