# listOf, arrayListOf, setOf, etc

**URL:** https://discuss.kotlinlang.org/t/listof-arraylistof-setof-etc/1475
**Category:** Libraries
**Created:** [February 15, 2016, 8:25pm UTC](https://discuss.kotlinlang.org/t/listof-arraylistof-setof-etc/1475 "2016-02-15T20:25:49Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Krupal](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@Krupal](https://discuss.kotlinlang.org/u/Krupal)
#### Post date: [February 15, 2016, 8:25pm UTC](https://discuss.kotlinlang.org/t/listof-arraylistof-setof-etc/1475/1 "2016-02-15T20:25:49Z")

</div>

I noticed that the functions above, and some others, are included by default in the global scope.

What Scala does is that these types would have to be prefixed explicitly. For example:

```
val m = Map.empty[String, String]
val s = Set(1, 2, 3, 4)

```

and so on.

Why didn’t Kotin adapt a similar approach? Something like

```
val s = Set.of(1, 2, 3)

```

instead of

```
val s = setOf(1, 2, 3)

```

Thanks

---

<div class="post-metadata">

### Author: ![jayson.minard](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jayson.minard/32/1434_2.png) [@jayson.minard](https://discuss.kotlinlang.org/u/jayson.minard)
#### Post date: [February 16, 2016, 12:37pm UTC](https://discuss.kotlinlang.org/t/listof-arraylistof-setof-etc/1475/2 "2016-02-16T12:37:16Z")

</div>

It was a design decision that more fits the personality of Kotlin. It is subjective. Kotlin does not use static factories in the stdlib anywhere. and in longer name cases like `Delegates.lazy { ... }` it looks more silly than just `lazy { .. .}`

Not sure why matters, Scala is not the role model for Kotlin and there will be different design choices. Which has been a good thing so far.

---

<div class="post-metadata">

### Author: ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)
#### Post date: [February 16, 2016, 5:53pm UTC](https://discuss.kotlinlang.org/t/listof-arraylistof-setof-etc/1475/3 "2016-02-16T17:53:32Z")

</div>

We have considered such naming style, for example `String.build { }` vs `buildString { }`, and found that the former, while it seems more logically “scoped”, is harder to explore via code completion, and the dot in the middle is kinda obstacle in typing process.

So given how frequent these functions are used, we decided to keep them top-level. For less frequent ones, such as `Regex.fromLiteral()` the “companion-scoped” approach fits ok.

Another consideration is that Java 9 may provide the similar but slightly different functions to instantiate List, Set and Map: those `List.of`, `Set.of` and `Map.of` ([JEP 269](http://openjdk.java.net/jeps/269)). This difference in their contract might become very confusing shall we choose the same names.

---

<div class="post-metadata">

### Author: ![Krupal](https://avatars.discourse-cdn.com/v4/letter/k/f0a364/32.png) [@Krupal](https://discuss.kotlinlang.org/u/Krupal)
#### Post date: [February 16, 2016, 6:32pm UTC](https://discuss.kotlinlang.org/t/listof-arraylistof-setof-etc/1475/4 "2016-02-16T18:32:33Z")

</div>

Thanks @ilya.gorbunov, that makes sense.

What is the reason then to make those functions return `LinkedHashSet` and `LinkedHashMap` as default instead of `HashSet` or `HashMap`, as the former use up more memory? Is there a way to change the default behavior if needed?

Thanks

---

<div class="post-metadata">

### Author: ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)
#### Post date: [February 18, 2016, 2:20am UTC](https://discuss.kotlinlang.org/t/listof-arraylistof-setof-etc/1475/5 "2016-02-18T02:20:47Z")

</div>

Long time ago we defaulted all maps and sets to be linked because it was really confusing to loose order in many practical applications.  
If you want a hashset for performance/memory reasons you can obtain it with `hashSetOf()` or `toHashSet()` functions.  
You cannot change the default behavior of `setOf` in the standard library, but if you really want, you can hide it with your own explicitly imported `setOf` function (`import my.utils.setOf`), though I wouldn’t recommend it as a long term solution.
