From the various online articles on Java 7 I have come to know that Java 7 will be having collection literals1 like the following:
List
You are right this is just empty syntactic sugar.
Also your ArrayList.of(...)
would be just short hand for new ArrayList<...>(Arrays.asList(...))
Another thing to note is that for lists this is very easy using var args, but have a little think about how you'd do it for a map. There is no way to supply pairs of arguments to a var args method, unless you introduce an extra Map.Entry object or something like that.
So using existing syntax you'd end up with
Map<String,String> map = HashMap.of( new Entry( "key1", "value1" ),
new Entry( "key2", "value2" ) );
which would get very tiring very quickly.
Even if you go down the path of using a builder pattern (as we do) then it's pretty ugly
Map<String,String> map = CollectionUtil.<String,String>map()
.put( "key1", "value1" )
.put( "key2", "value2" )
.map();
They might have been inspired by the declarative programming style of JavaFX. If that is the case however, I'm disappointed that they didn't go all the way to support object literals as well. Here's an example of object literals in JavaFX:
Stage {
title: "Group-Nodes-Transformation"
scene: Scene {
width: 600
height: 600
content: Group {
content: [
Circle {
centerX: 300
centerY: 300
radius: 250
fill: Color.WHITE
stroke: Color.BLACK
},
Text {
x: 300
y: 300
content: "Mr. Duke"
},
ImageView {
image: Image {
url: "D:\\TEST\\Documents\\duke.png"
width:50
height:50
}
}
]
}
}
}
I think that this style of programming certainly lends itself to certain application areas. In the end it's always a matter of taste...
Language design is a matter of taste. Having said that, this kind of list/object initialization has become very popular. For example, C# has almost the same thing. The syntax is quite flexible and, as even your examples show, lets you initialize something like a dictionary inline. I rather like the syntax the designers chose here.
It's not entirely new syntax as such literals are present in Python, Javascript (json) and probably other languages.
I haven't seen any information on java 7 yet to be frank (were focused on C++ and Javascript lately), but it's actually good to see such an addition to the language.
Answer to question 2:
final List<Integer> piDigits = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 9];
gives you an immutable list.
The whole idea of this proposal that the subtype cannot be specified. The compiler chooses a suitable implementation depending on the collection on the right-hand side.
Read the details here: Proposal: Collection Literals
Answer to question 1: yes it would have. It's a matter of style.