简体   繁体   English

Java - 当枚举本身可以用作地图时,为什么要使用 EnumMap?

[英]Java - Why use EnumMap when enum itself can be used as a map?

I've been reading up Java recently.我最近一直在阅读 Java。 So I have a enum below that acts as a map with key "JESSIE" and value "My favorite cat".所以我在下面有一个枚举,它充当带有键“JESSIE”和值“我最喜欢的猫”的地图。

So why does anyone need to use EnumMap?那么为什么有人需要使用 EnumMap 呢? Thanks谢谢

public enum Cat {
    JESSIE("My favorite cat");

    private String description;

    Cat(String description){
        this.description = description;
    }
}

The primary reason behind this would be to design the classes in such a way that it holds the attributes that represent its entity and not which it would require a mapping with while querying.这背后的主要原因是以这样一种方式设计类,即它拥有代表其实体的属性,而不是在查询时需要映射的属性。

As an example, consider another class Human as follows例如,考虑另一个类Human ,如下所示

@AllArgsConstructor
static class Human {
    String name;
}

Now you could have looked for a Cat to its owner mapping, but how much of a sense would it make to keep the entire Human object referenced for such a mapping?现在,您可以查找Cat到其所有者的映射,但是为这样的映射保留整个Human对象的引用有多大意义? Despite the fact, that you could keep such a reference, what's helpful for the purpose of query ability is to keep such a reference in an additional data structure EnumMap in this case -尽管事实上,您可以保留这样的引用,但在这种情况下,对查询能力有帮助的是将这样的引用保留在附加数据结构EnumMap中 -

EnumMap<Cat, Human> enumMap = new EnumMap<>(Cat.class);
enumMap.put(Cat.JESSIE, new Human("naman"));
enumMap.put(Cat.LUCY, new Human("user"));

The enum is the key in an EnumMap , and therefore those two things are completely different. enumEnumMap关键,因此这两件事是完全不同的。 You have no way of knowing all the kinds of things you may want to associate with your cats.您无法了解您可能想与您的猫联系在一起的所有事物。

The description may be an inherent part of Cat , but you might want to associate cats with their servants, or where they (currently) live or what their preferred meal (currently) is. description可能是Cat的固有部分,但您可能希望将猫与它们的仆人、它们(当前)住在哪里或它们(当前)喜欢的食物联系起来。

EnumMap<Cat, Human> catsServant;
EnumMap<Cat, House> catsHome;
EnumMap<Cat, Meal> catsFood;

Since you don't need to modify Cat to associate it with another object, it makes it a lot easier to use, and your enum would become huge if you filled it with all possible things you might want to associate your cats with.由于您不需要修改Cat以将其与另一个对象相关联,因此它更易于使用,并且如果您将所有可能想要与您的猫相关联的东西填充它,您的枚举将变得很大。

A second issue is that enum s are singletons, which means that if you were to put mutable state (strongly discouraged!) in your Cat by adding a setDescription(String) method, it will change that globally in your program.第二个问题是enum是单例,这意味着如果您要通过添加setDescription(String)方法将可变状态(强烈不鼓励!)放入Cat ,它将在您的程序中全局更改。 That may not matter for simple programs or a simple property like description , but it does matter when you have more complex code.对于简单的程序或像description这样的简单属性,这可能无关紧要,但是当您拥有更复杂的代码时,这很重要。

Now a more realistic example.现在是一个更现实的例子。 The JDK TimeUnit enum has values such as MINUTE , and the creators couldn't have known of all the possible things that people might want to associate with them. JDK TimeUnit枚举具有诸如MINUTE值,并且创建者不可能知道人们可能想要与它们关联的所有可能事物。 However with an EnumMap I can provide a translation to my native language as follows:但是,使用EnumMap我可以提供我的母语的翻译,如下所示:

EnumMap<TimeUnit, String> xlate = new EnumMap<>(TimeUnit.class);
xlate.put(TimeUnit.MINUTE, "Minuutti");
xlate.put(TimeUnit.SECOND, "Sekunti");

TimeUnit isn't my class, so I can't edit it to include those translations, and it would be the wrong place anyway. TimeUnit不是我的课程,所以我无法编辑它以包含这些翻译,无论如何它都是错误的地方。

A related question is why is there a special EnumMap , since you can also use a normal map, such as HashMap<Cat, Human> .一个相关的问题是为什么有一个特殊的EnumMap ,因为您也可以使用法线贴图,例如HashMap<Cat, Human>

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

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