Java Collections. Good stuff, but the API just didn't advance with time:
Map.get(Object)
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?
newArrayList<String>("foobar");
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.
Java Collections. Good stuff, but the API just didn't advance with time:
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?Nope, the constructor does not take items as parameter. Instead, use
Collections.singletonList
, but that one is immutable. Be surprised by your nextRuntimeException
. You could also useArrays.asList
, but not for a single item because that's considered inefficient.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...