As part of the JDK 9 proposals, I am not too sure about this, but there was some discussion going on over
Factory Methods for Collections
Such as , Collection literals that would allow the initialization of a collection with a compact expression as depicted here
for example creating a new Array in programming language Ruby can be done in the following ways:
new_array = Array.new
new_array = 
new_array = ["a", "b", "c"]
new_array = %w[a b c]
If written, it should increase productivity and, with shorter lines of code, increase clarity as the resulting object could be immutable.
Would appreciate your thoughts on this.
Whereas in C# I could do this:
I would be very happy if this feature came to Java.
Stephan van Hulst wrote:So I give up on generic type inference AND my application introduces a new class?
Yes, that's the price. Just wrote as an existing possibility.
my 2 cents :
List<Integer> list = #[ 1, 2, 3 ];
JEP 269: Convenience Factory Methods for Collections
Define library APIs to make it convenient to create instances of collections
and maps with small numbers of elements, so as to ease the pain of not
having collection literals in the Java programming language
• Decrease the amount of code needed for creating small collections and
core-libs / java.util:collections
Set<String> alphabet = Set.of("a", "b", "c");
Kenneth A. Kousen wrote:I think it'll be interesting to see how that affects the way developers code -- they're not necessarily expecting immutable collections from a method like that.
It shouldn't be much surprising though. More recent languages as Scala, Kotlin also does that. However, both languages have both, mutable and immutable collections. As a general practice immutable are preferred. And that is probably also not so surprising as nowadays paradigm heading towards more and more functional programming. As far as I understand Java 9 will support both too, standard way of creating will give a mutable, and using factory methods will lead to immutable.
I agree that it makes sense and even that it's a good thing. I think it's going to surprise a lot of developers though.
Kenneth A. Kousen wrote:Sure, but nothing about the name of the factory method implies that the resulting collection is immutable. For example, I used Arrays.asList(...) for years without realized that also produced an immutable collection.
It's not immutable, you can still replace values. You just can't add or remove anything.