I don't know if this is the right place to ask thi...
# announcements
a
I don't know if this is the right place to ask this but here we go. Can someone deeply explain to me the difference between
listOf<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>()
j
llistOf() returns a List. You can’t modify a List, and the returned implementation depends on how many elements you passed to listOf() (but you don’t need to care about what the implementation is: it’s a List, and that’s all you need to know) arrayListOf() returns an ArrayList. ArrayList is a specific implementation of MutableList, which is backed by an array. So it’s a List, but it’s also a MutableList: you can modify it. And you’re guaranteed to get an ArrayList (since that’s what it returns), so you know how the list works internally when you remove, add, insert or iterate over the elements of the list (by reading the documentation of ArrayList). The same goes for setOf() vs. hashSetOf()or linkedSetOf(). The former returns an immutable Set whose only guarantee (besides the guarantees common to all Sets) is that its elements are iterated in the given order, whereas the latters return specific implementations of MutableSet: HashSet and LinkedHashSet. Same again for mapOf, hashMapOf() and linkedMapOf(). Why there isn’t a linkedListIf(): probably because using a LinkedList is basically always a wrong thing to do: it’s less efficient and less memory efficient than array-backed lists.
a
Thank you so much for your explanation. It does help understand.
although I am even asking myself more now . . . What differs in their implementation.
You said an
arrayListOf()
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()
?
r
ArrayList is a specific implementation of a MutableList (which is also a List) Both List and MutableList are interfaces. An interface it’s just a public API (a set of methods you can use) but says nothing about the internals, how the information is stored in memory or how the operations are handled. A List is just a bunch of elements that cannot be changed, and a MutableList is another bunch of elements, but you can add or remove them. Any list or mutableList internally can be represented in different ways. You can have an array, or you can have nodes pointing to each other. When you use arrayList or linkedList you are making a conscious chose about these implementations (usually because you, as developer know they will perform better for your purposes). Usually you do not need to take that decision, so you let the compiler choose for you (when using listOf() or mutableListOf()). Or even if you need to take that decision, you want to keep that hidden in the place where that list is instantiated, but hide that information to other classes consuming this instance, that’s why you usually want declare it as List or MutableList, and keep the specific implementation just to where is important to know it. I think before going further into this, you first need to have a clear understanding about what is a interface, a class and some OOP concepts 🙂 These articles look like a good starting point: https://dev.to/lefebvre/oop-101-understanding-classes-343p https://www.smashingmagazine.com/2019/11/guide-oop-inheritance-interfaces-abstract-classes/
a
Hi @rolgalan, thank you for your response and time for this. I can say I do understand the concept of OOP. So I can tell between interfaces and class implementation. My questing was targeted more to the stdlib api and the stdlib implementation of those interfaces. You have successfully explained to me that an
ArrayList
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?
I took a deep dive at these collection constructor,
listOf()
,
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)
r
As far as I can tell, 
mutableListOf()
 and 
arrayListOf()
 have no difference in implementation at all.
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 that
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.
why is there no constructor function for 
linkedListOf
 in the stdlib?
This 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 the
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.
a
I have narrowed down my question to this, what is the difference between the implementation of a
HashSet
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?
d
LinkedHashSet
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
a
@rolgalan Thank you so much for your explanation
👍 2
@diesieben07 That's good to know. Is it safe to assume that it is same for
HashMap
and
LinkedHashMap
?
d
Yes,
HashSet
and
LinkedHashSet
are based on
HashMap
and
LinkedHashMap
respectively
a
@diesieben07 you explanations are short and clear. Thank you so much. I think I do understand now
d
As for `LinkedList`: I don't know if there is an official answer, but generally you hardly ever need
LinkedList
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.
j
These are all Data Structures concepts. You need to study Data Structures to understand the differences in implementation and when to use one over the other.
☝️ 1