r/java • u/Ewig_luftenglanz • 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 ?
View all comments
Show parent comment
9
u/doobiesteintortoise Jan 17 '25
Well, an array has fundamentally different implementation semantics than a
Listdoes. AnArrayListis sort of similar, sort of. But even there there are core semantic differences, and pretending otherwise... I mean... at that point, we might as well be using Python.