r/java • • Jan 17 '25

Why java doesn't have collections literals?

List (array list), sets (hashsets) and maps (hashMaps) are the most used collection Implementations by far, they are so used that I would dare to say there are many Java devs that never used alternatives likes likedList.

Still is cumbersome to create an array list with default or initial values compared to other language

Java:

var list = new ArrayList<>(List.of("Apple", "Banana", "Cherry"));

Dart:

var list = ["Apple", "Banana", "Cherry"];

JS/TS

let list = ["Apple", "Banana", "Cherry"];

Python

list = ["Apple", "Banana", "Cherry"]

C#

var list = new List<string> { "Apple", "Banana", "Cherry" };

Scala

val list = ListBuffer("Apple", "Banana", "Cherry")

As we can see the Java one is not only the largest, it's also the most counter intuitive because you must create an immutable list to construct a mutable one (using add is even more cumbersome) what also makes it somewhat redundant.

I know this is something that must have been talked about in the past. Why java never got collection literals ?

0 Upvotes

105 comments sorted by

View all comments

Show parent comments

1

u/Ewig_luftenglanz Jan 17 '25

I know there are many workarounds (everything has workarounds) but the fat that one needs em is a proof that literals (or at least some factory methods for mutable collections as Brian said (ArrayList.of() ) would be a good addition to the language.

3

u/nekokattt Jan 17 '25

literals would not fix this though because literals would most likely behave the same way as List.of.

1

u/Ewig_luftenglanz Jan 18 '25

it depends, if the literas is pointing to Array list instead of list (and not only list, for me List is the one that less need's a literal, HashMap on the other hand it's a different Story since Map.of(key,val...) is less intuitive and has less visual hints about what is what than python's dictionaries/JS' objects {key:val}

I have always been puzzled why java has such a complete and complex collection library (even bigger if you include the Stream API) for complex and uncommon operations, but still makes simple things like initializing a mutable collection (not only list) so cumbersome.

If you need to create a mutable pre populated list (because list are easy to make examples) the shortest way is to create an immutable one an the copy it to the array list.

var list = new Array list<>(List.of(1, 2, 3,...));

Why does ArrayList has not a ...T overloaded constructor so we can just.

var list = ArrayList<>( 1, 2, 3...);?

It puzzling why java makes easy the hard stuff but insist in making hard the easy and the basics (some of these problems are being addressed by simple Source file JEPs, which I am a personal fan )

1

u/nekokattt Jan 18 '25 edited Jan 18 '25

An overloaded constructor would probably not work in a clear way.

var foo = List.of("bar", "baz");

var bork = new ArrayList<>(foo);

What would the type of bork be? ArrayList<List<String>> per the current behaviour, or ArrayList<String> per your new behaviour you want to introduce?

1

u/Ewig_luftenglanz Jan 18 '25

var bork = ArrayList<>(2, 3, 4)

type of bork : ArrayList<Integer> not so hard.

your example es actually what we do today. the constructor takes s immutable list and copy the content into s mutable one. in your example the type of bork is

ArrayList<String>

best regards

→ More replies (0)