andylamax
07/26/2020, 6:09 AMlistOf<T>()
and arrayListOf<T>
. And while we are at it I would like to know more about the differences of
1. setOf<T>()
and hashSetOf<T>()
and linkedSetOf<T>()
2. mapOf<T>()
and hashMapOf<T>()
and linkedMapOf<T>()
Also why, List<T>
has no factory methods called linkedListOf<T>()
jbnizet
07/26/2020, 7:16 AMandylamax
07/26/2020, 9:39 AMandylamax
07/26/2020, 9:40 AMandylamax
07/26/2020, 9:42 AMarrayListOf()
is a mutable list, but the same goes for a mutableListOf()
. At which scenerio would I need the former over the later? Same goes to mutableSetOf()
vs hashSetOf()
vs linkedSetOf()
?rolgalan
07/26/2020, 11:10 AMandylamax
07/26/2020, 11:49 AMArrayList
is backed by an array
and a linkedList
is sequence of nodes each pointing to the next node until there is no node to point.
however, my question deliberately asked, why is there no constructor function for linkedListOf
in the stdlib?
also, are how are linkedHashMap
implemented?
You also said, "I need to make a decision on which to chose", this is correct. But In order to do so, I need to understand the implementations of each for me to have the ability to choose correctly. What are the different implementations?andylamax
07/26/2020, 11:56 AMlistOf()
, mutableListOf()
,arrayListOf()
. they all return a List
, MutableList
and ArrayList
respectively but they are all casted from an ArrayList
implementation. So apart from mutability reasons, there is no difference between using listOf()
and mutableListOf()
they are all implemented by an ArrayList
.
As far as I can tell, mutableListOf()
and arrayListOf()
have no difference in implementation at all. (Please correct me if I am wrong)rolgalan
07/26/2020, 12:16 PMAs far as I can tell,You are right. This was a design decision took by the Kotlin team. Probably they noticed that most common usages of lists in day to day development are better handled by an ArrayList. The difference is thatandmutableListOf()
have no difference in implementation at all.arrayListOf()
mutableListOf()
is currently returning an ArrayList implementation, but that might change in the at any moment. (Of course that would be a tough decision from JB and should be well communicated, but the API allows this).
However, arrayListOF()
will never return any other list implementation different than ArrayList
When you use to arrayListOf()
you are explicitly saying that an ArrayList is what you need and what better suits your requirements; your code says “I want a list backed in an array!“. When you use mutableListOf()
your code is saying: “hey, I just want a list, don’t care how, just give me anything where I can add and remove stuff.”
About how they are implemented inside… I’m not sure about all of them, you should check the sourcecode for that. The Colections Framework Java guide could be quite helpful to have a clear vision of all of them.rolgalan
07/26/2020, 12:21 PMwhy is there no constructor function forThis is a Kotlin design team decision. I guess they wanted to provide an easy way to instantiate implementation-unaware lists, so they came up with thein the stdlib?linkedListOf
listOf
mutableListOf
and similar functions to give you any list implementation without caring about which one is it. And probably this was helpful also for the multiplatform project, letting each final platform to choose which specific implementation use.andylamax
07/26/2020, 12:38 PMHashSet
and a LinkedHashSet?
Also the difference between the implementation of a HashMap
and LinkedHashMap
And why did the kotlin team decide not to include a LinkedList
in the kotlin stdlib?diesieben07
07/26/2020, 12:40 PMLinkedHashSet
preserves iteration order.
If you do add(1)
and then add(2)
and then iterate through the set you will get 1
then 2
with LinkedHashSet
. With HashSet
you might get any order. To do this LinkedHashSet
does use more memory than a plain HashSet
andylamax
07/26/2020, 12:40 PMandylamax
07/26/2020, 12:43 PMHashMap
and LinkedHashMap
?diesieben07
07/26/2020, 12:44 PMHashSet
and LinkedHashSet
are based on HashMap
and LinkedHashMap
respectivelyandylamax
07/26/2020, 12:47 PMdiesieben07
07/26/2020, 12:53 PMLinkedList
and ArrayList
is almost always a better choice in terms of performance and memory-usage. One use-case (queues) is now also covered in the stdlib using ArrayDeque.jordigarcl
07/26/2020, 6:05 PM