来自 Guava 或 Java 枚举的 ImmutableSet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15952128/
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
ImmutableSet from Guava or Java enum
提问by mat_boy
I read herea nice example about using ImmutableSetfrom Guava. The example is reported here for the sake of completeness:
我在这里阅读了一个关于使用ImmutableSetGuava的很好的例子。为完整起见,此处报告该示例:
public static final ImmutableSet<String> COLOR_NAMES = ImmutableSet.of(
"red",
"orange",
"yellow",
"green",
"blue",
"purple");
class Foo {
Set<Bar> bars;
Foo(Set<Bar> bars) {
this.bars = ImmutableSet.copyOf(bars); // defensive copy!
}
}
The question is, can I obtain the same result by using a Java enum?
问题是,我可以通过使用 Java 枚举获得相同的结果吗?
PS: This questionadded into my mind more chaos!
PS:这个问题让我脑子里更乱了!
回答by Xaerxess
Can I obtain the same result by using a Java enum?
我可以通过使用 Java 枚举获得相同的结果吗?
Yes, you can. Did you try it?
是的你可以。你试了吗?
FYI There's also specialized version of ImmutableSetwhich holds enum's constants - Sets.immutableEnumSet(internally it uses EnumSet).
仅供参考还有专门的版本,ImmutableSet其中包含枚举的常量 - Sets.immutableEnumSet(在内部使用EnumSet)。
Some examples (paraphrasing Wiki examples):
一些例子(解释维基例子):
public class Test {
enum Color {
RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE;
}
static class Baz {
ImmutableSet<Color> colors;
Baz(Set<Color> colors) {
this.colors = Sets.immutableEnumSet(colors); // preserves enum constants
// order, not insertion order!
}
}
public static void main(String[] args) {
ImmutableSet<Color> colorsInInsertionOrder = ImmutableSet.of(
Color.GREEN, Color.YELLOW, Color.RED);
System.out.println(colorsInInsertionOrder); // [GREEN, YELLOW, RED]
Baz baz = new Baz(colorsInInsertionOrder);
System.out.println(baz.colors); // [RED, YELLOW, GREEN]
}
}
EDIT (after OP's comment):
编辑(在 OP 评论之后):
Do you want all enum constants in ImmutableSet? Just do:
你想要 ImmutableSet 中的所有枚举常量吗?做就是了:
Sets.immutableEnumSet(EnumSet.allOf(Color.class));
回答by matsev
No, not quite. Compare
不,不完全是。比较
public enum Color {
RED, ORANGE, YELLOW, GREEN, BLUE, PURPLE;
}
Set<Color> colors = EnumSet.allOf(Color.class);
with
和
Set<String> colors = ImmutableSet.of(
"red", "orange", "yellow", "green", "blue", "purple"
);
Since Java is statically typed, you will have a Set<Color>in the first example, and a Set<String>in the latter example.
由于 Java 是静态类型的,因此您将Set<Color>在第一个示例中使用 a Set<String>,在后一个示例中使用 a。
Edit 1
编辑 1
Another difference is that you can create ImmutableSetof arbitrary size in runtime (provided that no single element equals()any of the other elements). In contrast, an EnumSetcan also be created during runtime, but it can never contain more elements than the number of enum values.
另一个区别是您可以ImmutableSet在运行时创建任意大小(前提是没有单个元素与equals()任何其他元素)。相比之下, anEnumSet也可以在运行时创建,但它包含的元素永远不能超过枚举值的数量。
Edit 2
编辑 2
An ImmutableSetmay contain elements of different classes, as long as they implement the same interface. An EnumSetcan only contain the enum type.
一个ImmutableSet可以包含不同类的元素,只要它们实现相同的接口。AnEnumSet只能包含枚举类型。
回答by WeGa
If you don't have all these fancy utility libraries as dependecies, you can use standard way:
如果你没有所有这些花哨的实用程序库作为依赖,你可以使用标准方式:
enum Furniture{SOFA, CHAIR, TABLE};
Set<Furniture> set = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(Furniture.values())));
BTW: isn't it the most efficient way? I saw a lot of code inside those library's methods, probably it is overkill? Anyway, it depends on context. My approach is a bit verbose and don't do some optimizations like cacheing and so on, but it is independent and exactly what OP is asking for.
BTW:这不是最有效的方式吗?我在这些库的方法中看到了很多代码,可能是矫枉过正?无论如何,这取决于上下文。我的方法有点冗长,不做一些优化,如缓存等,但它是独立的,正是 OP 所要求的。

