Why does `arrayListOf` and `listOf`exist? Both seem to be of Type ArrayList?
c
Why does
arrayListOf
and `listOf`exist? Both seem to be of Type ArrayList?
a
Hi @Colton Idle ,
listOf
gives you list so it read only But
arrayListOf
give you mutable list so you can add and delete and edit the items
c
So what's
mutableListOf
needed if we have
arrayListOf
?
m
I think
arrayListOf
is just there for consistency.
listOf
and
mutableListOf
should be preferred unless there is a specific data structure you need.
3
j
As Matthew said, only use
arrayListOf
if you have a specific need for an
ArrayList
. Most of the time, a general mutable list should be sufficient and thus most of the time you should only need
mutableListOf
It's the same idea as using interfaces for your types rather than specific implementation classes. Usually you should prefer to pass
List
around, or
MutableList
if you really need the mutability, but you should almost never need to pass an
ArrayList
around
s
listOf
is a function that returns an
List
implementation, by default in JVM this is
ArrayList
arrayListOf
is a function that returns the explicit list implementation
ArrayList
ArrayList
is just an implementation of list were the data is contiguous and mutable By default, try to use
mutableListOf
or
listOf
it will allow you to pass any class that implements those interfaces (this will depend of your needs)
c
Gotcha. So listOf in other environments could possibly return a different type.
👍 1
c
In general, you should write your code so that it doesn’t care about the type of List you’re using. You really only care about it being a
MutableList
or
List
, but nothing more than that, because any more specificity unnecessarily couples your code to a specific implementation of
List
which makes it difficult to use. Furthermore,
listOf()
actually does return different List subclasses based on how many elements you pass to it, which may or may not be
ArrayList
. For example,
listOf()
returns the
internal object EmptyList
,
listOf(1)
returns
Collections.singletonList
, and everything else returns
ArrayList
. There’s really not a good reason to use
arrayListOf()
vs
listOf()
. I suspect it’s mostly there for historical reasons, because older Java code sometimes would declare function parameters, etc. as
ArrayList
rather than
List
. When interop-ing with that code, you can’t just cast
listOf()
to
ArrayList
because of how that function is optimized, so
arrayListOf()
gives you a way to know you’re getting exactly an ArrayList to work with those APIs.
👍 3
👆 1
e
there are a few methods available on
ArrayList
that aren't on
MutableList
, e.g. to manage capacity. so in some circumstances it may be useful, even beyond Java compatibility
👍 1
k
Besides the "somethingOf()" functions, it's also possible to create lists using functions that look like constructors, for example
List(5) { it * 2
creates the same list as
listOf(0, 2, 4, 6, 8)
and can be useful if you want to create large lists.
j
These constructors are also super useful to parse input in programming competitions such as Hashcode or Codejam