DEV Community

Discussion on: What simple things annoy you about your favourite programming languages?

Collapse
 
joshavg profile image
Josha von Gizycki • Edited

Java Collections. Good stuff, but the API just didn't advance with time:

Map.get(Object)
Enter fullscreen mode Exit fullscreen mode

You never know for sure if your key is a valid parameter, even if your generics tell, which key type you want to use. Always Object.

Want to create an ArrayList with one entry? Think this works?

new ArrayList<String>("foobar");
Enter fullscreen mode Exit fullscreen mode

Nope, the constructor does not take items as parameter. Instead, use Collections.singletonList, but that one is immutable. Be surprised by your next RuntimeException. You could also use Arrays.asList, but not for a single item because that's considered inefficient.

Collapse
 
val_baca profile image
Valentin Baca

YES. This is very annoying.

They're not part of the core language and you probably already know about them, but for those who don't:

Google Guava has Lists: Lists.newArrayList(E... elements)

Implementation: github.com/google/guava/blob/maste...

And (of course) Kotlin has the lovely arrayListOf(...)

kotlinlang.org/api/latest/jvm/stdl...