In Hidden Features of Java the top answer mentions Double Brace Initialization , with a very enticing syntax:
(在Java的隐藏功能中 ,最佳答案提到了Double Brace Initialization ,它具有非常诱人的语法:)
Set<String> flavors = new HashSet<String>() {{
add("vanilla");
add("strawberry");
add("chocolate");
add("butter pecan");
}};
This idiom creates an anonymous inner class with just an instance initializer in it, which "can use any [...] methods in the containing scope".
(这个成语创建了一个匿名内部类,其中只包含一个实例初始化程序,“可以使用包含作用域中的任何[...]方法”。)
Main question: Is this as inefficient as it sounds?
(主要问题:这听起来效率低吗?)
Should its use be limited to one-off initializations?(它的使用是否应限于一次性初始化?)
(And of course showing off!)((当然炫耀!))
Second question: The new HashSet must be the "this" used in the instance initializer ... can anyone shed light on the mechanism?
(第二个问题:新的HashSet必须是实例初始化程序中使用的“this”...任何人都可以了解机制吗?)
Third question: Is this idiom too obscure to use in production code?
(第三个问题:在生产代码中使用这个成语是否过于模糊 ?)
Summary: Very, very nice answers, thanks everyone.
(简介:非常非常好的答案,谢谢大家。)
On question (3), people felt the syntax should be clear (though I'd recommend an occasional comment, especially if your code will pass on to developers who may not be familiar with it).(在问题(3)中,人们认为语法应该是清楚的(尽管我建议偶尔发表评论,特别是如果你的代码会传递给可能不熟悉它的开发人员)。)
On question (1), the generated code should run quickly.
(在问题(1)上,生成的代码应该快速运行。)
The extra .class files do cause jar file clutter, and slow program startup slightly (thanks to @coobird for measuring that).(额外的.class文件会导致jar文件混乱,并且会稍微减慢程序启动速度(感谢@coobird测量它)。)
@Thilo pointed out that garbage collection can be affected, and the memory cost for the extra loaded classes may be a factor in some cases.(@Thilo指出垃圾收集可能会受到影响,在某些情况下,额外加载类的内存成本可能是一个因素。)
Question (2) turned out to be most interesting to me.
(问题(2)对我来说最有趣。)
If I understand the answers, what's happening in DBI is that the anonymous inner class extends the class of the object being constructed by the new operator, and hence has a "this" value referencing the instance being constructed.(如果我理解答案,那么DBI中发生的事情是匿名内部类扩展了由new运算符构造的对象的类,因此具有引用正在构造的实例的“this”值。)
Very neat.(井井有条。)
Overall, DBI strikes me as something of an intellectual curiousity.
(总的来说,DBI让我感到非常好奇。)
Coobird and others point out you can achieve the same effect with Arrays.asList, varargs methods, Google Collections, and the proposed Java 7 Collection literals.(Coobird和其他人指出,您可以使用Arrays.asList,varargs方法,Google Collections和提议的Java 7 Collection文字获得相同的效果。)
Newer JVM languages like Scala, JRuby, and Groovy also offer concise notations for list construction, and interoperate well with Java.(Scala,JRuby和Groovy等较新的JVM语言也为列表构建提供了简明的符号,并且与Java良好地互操作。)
Given that DBI clutters up the classpath, slows down class loading a bit, and makes the code a tad more obscure, I'd probably shy away from it.(鉴于DBI使类路径混乱,减慢了类加载速度,并使代码更加模糊,我可能会回避它。)
However, I plan to spring this on a friend who's just gotten his SCJP and loves good natured jousts about Java semantics!(但是,我打算在一位刚刚获得SCJP的朋友身上发表这篇文章,并且喜欢关于Java语义的好朋友!)
;-) Thanks everyone!(;-) 感谢大家!)
7/2017: Baeldung has a good summary of double brace initialization and considers it an anti-pattern.
(7/2017:Baeldung对双支撑初始化有很好的总结 ,并认为它是一种反模式。)
12/2017: @Basil Bourque notes that in the new Java 9 you can say:
(12/2017:@Basil Bourque指出,在新的Java 9中你可以说:)
Set<String> flavors = Set.of("vanilla", "strawberry", "chocolate", "butter pecan");
That's for sure the way to go.
(这肯定是要走的路。)
If you're stuck with an earlier version, take a look at Google Collections' ImmutableSet .(如果您遇到早期版本,请查看Google Collections的ImmutableSet 。)
ask by Jim Ferrans translate from so