Java 用于实例化初始化集合的紧凑语法

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

Compact syntax for instantiating an initializing collection

javacollectionsinitialization

提问by Dónal

I'm looking for a compact syntax for instantiating a collection and adding a few items to it. I currently use this syntax:

我正在寻找一种紧凑的语法来实例化一个集合并向其中添加一些项目。我目前使用这种语法:

Collection<String> collection = 
    new ArrayList<String>(Arrays.asList(new String[] { "1", "2", "3" }));

I seem to recall that there's a more compact way of doing this that uses an anonymous subclass of ArrayList, then adds the items in the subclass' constructor. However, I can't seem to remember the exact syntax.

我似乎记得有一种更紧凑的方法可以使用 的匿名子类ArrayList,然后在子类的构造函数中添加项目。但是,我似乎无法记住确切的语法。

回答by Vivien Barousse

Maybe that was

也许那是

Collection<String> collection = new ArrayList<String>() {{
    add("foo");
    add("bar");
}};

Also known as double-bracket initialization.

也称为双括号初始化。

回答by Riduidel

I guess you're thinking about

我猜你在想

collection = new ArrayList<String>() { // anonymous subclass
     { // anonymous initializer
         add("1");
         add("2");
         add("3");
     }
}

which, one comapcted, gives

其中,一个压缩,给出

collection = new ArrayList<String>() {{ add("1"); add("2"); add("3"); }}

FUGLY, to say the least. However, there is a variant to the Arrays.asList method : Arrays.asList(T...a)which provides comapcity and readability. As an example, it gives the following line of code :

FUGLY,至少可以这么说。但是,Arrays.asList 方法有一个变体:Arrays.asList(T...a)它提供了兼容性和可读性。例如,它给出了以下代码行:

collection = new ArrayList<String>(Arrays.asList("1", "2", "3")); // yep, this one is the shorter

And notice you don't create an anonymous subclass of ArrayList of dubious use.

请注意,您没有创建可疑用途的 ArrayList 的匿名子类。

回答by posdef

Maybe it's just me but I dont see the point of complicating things, purely in pursuit of writing shorter/faster code. Given the choice of typing marginally fewer lines of code and far easier debugging/revising I am pretty sure I'd choose the second option.

也许这只是我,但我不认为将事情复杂化的意义,纯粹是为了编写更短/更快的代码。考虑到选择稍微少一点的代码行和更容易的调试/修改,我很确定我会选择第二个选项。

So unless there is a particular reason to push for keeping this short, I'd say stick to Ockham's razor, and given a number of solutions to a problem, go with the simplest one. That way when something does go wrong (which Murphy's law clearly dictates it will) you'll have a much easier time to track it. :)

因此,除非有特别的理由要求保持简短,否则我会说坚持使用奥卡姆剃刀,并给出许多解决问题的方法,选择最简单的方法。这样,当出现问题时(墨菲定律明确规定它会出错),您将有更轻松的时间来跟踪它。:)

回答by Haroldo_OK

You could create an utility function:

您可以创建一个实用程序函数:

@SafeVarargs
public static <T> List<T> listOf(T ... values) {
    return new ArrayList<T>(Arrays.asList(values));
}

So you could call it like:

所以你可以这样称呼它:

collection = MyUtils.listOf("1", "2", "3");

That way, you can populate a list very easily, and still keep it mutable.

这样,您可以非常轻松地填充列表,并且仍然保持可变。