java 番石榴中是否有相应的不可变 enumMap ?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11244402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:23:08  来源:igfitidea点击:

Is there a corresponding immutable enumMap in guava?

javaimmutabilityguavaenum-map

提问by brainydexter

I recently learnt about the benefits of EnumMap in Java and would like to replace the existing ImmutableMap<OccupancyType, BigDecimal>to EnumMap. However, I'd also like the immutable property offered by ImmutableMap.

我最近了解了 Java 中 EnumMap 的好处,并想将现有的替换ImmutableMap<OccupancyType, BigDecimal>为 EnumMap。但是,我也喜欢 ImmutableMap 提供的不可变属性。

  • Is there a variant, ImmutableEnumMap available in guava ?
  • In terms of storage which one (EnumMap vs ImmutableMap) performs better ?
  • I couldn't find a comparison of the two. I'd appreciate if someone can point me to a link or give some insights on the efficiency of the two data structures ?
  • 番石榴中是否有变体 ImmutableEnumMap 可用?
  • 在存储方面哪一个(EnumMap vs ImmutableMap)表现更好?
  • 我找不到两者的比较。如果有人可以指出我的链接或对两种数据结构的效率提供一些见解,我将不胜感激?

回答by Louis Wasserman

Guava contributor here.

番石榴贡献者在这里。

Guava doesn't currently have an ImmutableEnumMapvariant, but if it did, it would probably just be a wrapper around an EnumMap. (That said, slightly better immutable implementations are possible.)

Guava 目前没有ImmutableEnumMap变体,但如果有,它可能只是一个EnumMap. (也就是说,稍微好一点的不可变实现是可能的。)

EnumMapwill perform better than the basic ImmutableMap, in any event; it's difficult or impossible to beat.

EnumMapImmutableMap无论如何都会比基本的表现更好;很难或不可能被击败。

(I'll file an issueto investigate adding an ImmutableMapvariant for enum key types, though.)

(不过,我将提交一个问题来调查ImmutableMap为枚举键类型添加变体。)



Update: Guava 14 adds Maps.immutableEnumMap().

更新:番石榴 14 添加了Maps.immutableEnumMap().

回答by The Alchemist

I was just wanted to provide an example now that ImmutableEnumMapis in Guava 14.0, because it's a package-private class, so you can't do ImmutableEnumMap.of(). You have to do Maps.immutableEnumMap()instead.

我只是想提供一个现在ImmutableEnumMapGuava 14.0 中的例子,因为它是一个包私有类,所以你不能做ImmutableEnumMap.of(). 你必须这样做Maps.immutableEnumMap()

private final ImmutableMap<MyEnum, String> myEnumMap = Maps.immutableEnumMap(ImmutableMap.of(
        MyEnum.A,   "A",
        MyEnum.B,   "B",
        MyEnum.C,   "C"
        ));

Not sure if there's a more natural syntax.

不确定是否有更自然的语法。

回答by jmh

As the guava ImmutableEnumMap is still marked beta as of version 14, I would suggest using a unmodifiable view of a enum map and then throwing away the original reference to the enum map to ensure that it is immutable.

由于 guava ImmutableEnumMap 在版本 14 中仍被标记为 beta,我建议使用枚举映射的不可修改视图,然后丢弃对枚举映射的原始引用以确保它是不可变的。

Example (in a constructor):

示例(在构造函数中):

Map entries = new EnumMap <SomeEnum, T>(SomeEnum.class);
... // (fill in entries)
this.entries = Collections.unmodifiableMap(entries);