Java 如何初始化集合并在同一行添加数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19185085/
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
How can I initialize a collection and add data on the same line?
提问by
In C# I can create a collection of some kind and initialize it with data on the same line.
在 C# 中,我可以创建某种集合并使用同一行上的数据对其进行初始化。
var foo = new List<string> {"one","two","three"};
var foo = new List<string> {"one","two","three"};
Is there an equivalent way to do this in Java?
在 Java 中是否有等效的方法来执行此操作?
采纳答案by Ravi Thapliyal
If you need a read-onlyList
如果您需要只读List
List<String> numbers = Arrays.asList("one","two","three");
// Can't add since the list is immutable
numbers.add("four"); // java.lang.UnsupportedOperationException
If you would like to modifythe List
later on.
如果你想修改的List
对后世。
List<String> numbers2 = new ArrayList<String>(
Arrays.asList("one","two","three"));
numbers2.add("four");
System.out.println(numbers2); // [one, two, three, four]
回答by Nailgun
List<String> list = Arrays.asList("one","two","three")
回答by Daniel Imms
You can use Arrays.asList(T... a)
您可以使用 Arrays.asList(T... a)
List<String> foo = Arrays.asList("one","two","three");
As Boris mentions in the comments the resulting List
is immutable (ie. read-only). You will need to convert it to an ArrayList
or similar in order to modify the collection:
正如鲍里斯在评论中提到的那样,结果List
是不可变的(即只读)。您需要将其转换为 anArrayList
或类似的以修改集合:
List<String> foo = new ArrayList<String>(Arrays.asList("one","two","three"));
You can also create the List
using an anonymous subclass and initializer:
您还可以List
使用匿名子类和初始化程序创建:
List<String> foo = new ArrayList<String>() {
{
add("one");
add("two");
add("three");
}
};
回答by Suresh Atta
List<String> numbers = Arrays.asList("one","two","three");
As Boris commented, it makes your numbers
immutable.
正如鲍里斯所评论的那样,它使您无法numbers
改变。
Yes,You can, but with two lines.
是的,你可以,但有两行。
List<String> numbers= new ArrayList<String>();
Collections.addAll(numbers,"one","two","three");
If you still want in Only in one line ,With Gauva
如果您还想在 Only in one line 中使用Gauva
List<String> numbers= Lists.newArrayList("one","two","three");
回答by Ryan Ransford
The best that I've been able to come up with is:
我能想到的最好的是:
final List<String> foo = new ArrayList<String>() {{
add("one");
add("two");
add("three");
}};
Basically, that says that you are creating an anonymous sub-class of the ArrayList
class which is then statically initialized using "one", "two", "three"
.
基本上,这表示您正在创建该类的匿名子ArrayList
类,然后使用"one", "two", "three"
.
回答by Sebastiaan van den Broek
I prefer doing this using the Guava (formerly called Google Collections) library, which both removes the need to write the type down again AND has all kinds of ways of adding data straight away.
我更喜欢使用 Guava(以前称为 Google Collections)库来做这件事,它既不需要再次写下类型,又可以通过各种方式立即添加数据。
Example: List<YourClass> yourList = Lists.newArrayList();
例子: List<YourClass> yourList = Lists.newArrayList();
Or with adding data: List<YourClass> yourList = Lists.newArrayList(yourClass1, yourclass2);
或者添加数据: List<YourClass> yourList = Lists.newArrayList(yourClass1, yourclass2);
The same works for all other kinds of collections and their various implementations. Another example: Set<String> treeSet = Sets.newTreeSet();
这同样适用于所有其他类型的集合及其各种实现。另一个例子:Set<String> treeSet = Sets.newTreeSet();
You can find it at https://code.google.com/p/guava-libraries/