https://kotlinlang.org logo
#getting-started
Title
# getting-started
i

igor.wojda

09/06/2021, 6:16 AM
Hey What are you thought on using
Collection
type? Fine / Not Fine? and Why?
Copy code
val modifiers: Collection<Modifier> = emptyList()
t

Tomasz Krakowiak

09/06/2021, 6:29 AM
Depends strongly on use case. For example, if you want to make many
contains
calls on large collection, than it's better to convert it to some set implementation first, as some Collection implementation may be not efficient for that. But if you just want to write a function that would iterate over elements and do whatever, than it would be better to accept a `Collection`(or even
Sequence
), to remove the need of conversion of an arbitrary type of collection the caller uses to one that you use.
s

Stephan Schroeder

09/06/2021, 6:40 AM
Collection
has a
size
, which `Iterable`/`Sequence` doesn't.
List
provides an order of elements on top of
Collection
.
Set
provides a uniqueness guarantee on top of
Collection
.
Collection
would often be good enough (it's nice to iterate and to have the size, if only for log messages or something). But
List
is shorter to write, so I mostly use
List
even though I very often don't need any specific order 😅