如何在 Java 7 中使用集合文字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10705705/
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 02:11:46  来源:igfitidea点击:

How do I use collection literals in Java 7?

javacollections

提问by DaedalusUsedPerl

I've tried the following line:

我试过以下几行:

Map<Character, Color> map={new Character('r'):Color.red,new Character('b'):Color.black};

But Netbeans 7 rejects this, with the error message '{' expected, ';' expected.

但是 Netbeans 7 拒绝了这一点,并显示了错误消息'{' expected, ';' expected

I've set the Source/Binary format as 'JDK 7'and the platform to 'JDK 1.7', is there anything else I need to do?

我已将源代码/二进制格式设置为“JDK 7”并将平台设置为“JDK 1.7”,还有什么我需要做的吗?

回答by Natix

Neither Java 7 nor Java 8 supports collection literals, as discussed in this question: Are Project Coin's collection enhancements going to be in JDK8?

Java 7 和 Java 8 都不支持集合文字,如以下问题中所述:Project Coin 的集合增强是否将在 JDK8 中出现?

You can use Google's Guavalibrary if you need only immutable collections. ImmutableList, ImmutableSetand ImmutableMaphave several overloaded factory methods or even builders that make creating collections easy:

如果您只需要不可变集合,则可以使用 Google 的Guava库。ImmutableList,ImmutableSetImmutableMap有几个重载的工厂方法甚至构建器,使创建集合变得容易:

List<Integer> list = ImmutableList.of(1, 1, 2, 3, 5, 8, 13, 21);
Set<String> set = ImmutableSet.of("foo", "bar", "baz", "batman");
Map<Integer, String> map = ImmutableMap.of(1, "one", 2, "two", 3, "three");

EDIT

编辑

Java 9 has added collection factory methodssimilar to those of Guava:

Java 9 添加了类似于 Guava 的集合工厂方法

List.of(a, b, c);
Set.of(d, e, f, g);
Map.of(k1, v1, k2, v2)

Map.ofEntries(
    entry(k1, v1),
    entry(k2, v2),
    entry(k3, v3),
    // ...
    entry(kn, vn)
);

回答by Thomas

You need to define a concrete map implementation, optionally combined with double brace initialization:

您需要定义一个具体的映射实现,可选择结合双括号初始化:

Map<Character, Color> map = new HashMap<Character, Color>() {{ 
  put(new Character('r'), Color.red);
  put(new Character('b'), Color.black );
}};

回答by Seth

To expand a little on Thomas's answer... Map is an interface, and must be instantiated through one of the associated concrete implementations (HashMap, TreeMap, or LinkedHashMap). It is still good practice; however, to declare your reference variable as the interface implementation rather than the specific concrete, as it provides future flexibility.

稍微扩展一下 Thomas 的回答... Map 是一个接口,必须通过关联的具体实现之一(HashMap、TreeMap 或 LinkedHashMap)进行实例化。这仍然是一种很好的做法;但是,将您的引用变量声明为接口实现而不是具体的具体实现,因为它提供了未来的灵活性。

Regarding the code snippet though, I think you do still need the Key-value pairs defined in the assignment side of the declaration. So, I would change:

不过,关于代码片段,我认为您仍然需要在声明的赋值端定义的键值对。所以,我会改变:

Map<Character, Color> map = new HashMap<>() {{ 

to

Map<Character, Color> map = new HashMap<Character, Color>() {{