News for hackers. Stuff that matters.
Re:Java 7 - What's new? Release date, code examples and perf... by _delirium on Sunday April 11, 2010 @06:28PM (#1257787)
The compiler chooses, probably based on the type and number of elements. In theory it should know how to do this efficiently, because this syntax returns only immutable collections, so the compiler knows up front exactly what the collection will ever contain.

This is what the proposal says about that decision:

  It is likely to be higher in space and time performance, in that the
  compiler can choose efficient implementations for small collections
  (such as empty collections and singletons).  On the other hand, it is
  less flexible, in that you cannot specify the implementation type.
From: http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/0...
Re:Java 7 - What's new? Release date, code examples and perf... by abalashov on Sunday April 11, 2010 @08:20PM (#1258012)
Why does it only return immutable collections? Because the spec says so, or because there is some principle suggesting that it should?

Personally, my first instinct was to read the syntax as a form of initialisation, not something that would bar me from adding or deleting elements after the set is instantiated.

Re:Java 7 - What's new? Release date, code examples and perf... by extension on Sunday April 11, 2010 @11:46PM (#1258354)
Because it solves the problem mentioned above i.e. the compiler's choice of which type of container to create becomes straightforward.
Re:Java 7 - What's new? Release date, code examples and perf... by sesqu on Monday April 12, 2010 @06:13AM (#1258819)
If the choice would be a mutable one, making it expressly immutable after the fact serves no purpose. As it is, the compiler gets to choose an efficient list for the data provided, and then refuse to compromise on that efficiency later. You might as well use an enum. What useful collection implementation would require immutability? Even arrays are more flexible.

Sure, it's nice syntax, but I can come up with more scenarios where I'll want to initialize a collection than scenarios where I want to finalize one (well, excepting for the fact that this is java, where immutability is often considered a virtue).

Re:Java 7 - What's new? Release date, code examples and perf... by mhansen on Monday April 12, 2010 @12:20AM (#1258393)
How does this maintain the List interface, which defines methods for mutating the collection through methods such as add() and remove()?

If I get a variable of type List, I expect to be able to change the list.

Re:Java 7 - What's new? Release date, code examples and perf... by _delirium on Monday April 12, 2010 @01:11AM (#1258461)
The List interface labels all the methods that could change a list as "optional operation", which implementing classes are free to throw an UnsupportedOperationException for: http://java.sun.com/javase/6/docs/api/java/util/List.html

The current API already uses this in a bunch of places to return immutable Lists. For example, the lists you get out of Collections.singletonList() or Collections.nCopies() are immutable.

That's a bit broken from an OO perspective (is the interface an interface or not?), but it's a better solution than the const v. non-const container mess that C++/STL has, at least.