What is a difference among different way of collections declaration?
I want to know the difference between:
arrayandarrayOfhashmapandhashmapOf
What is a difference among different way of collections declaration?
I want to know the difference between:
array and arrayOfhashmap and hashmapOfArray and Hashmap are constructors of the classes Array and Hashmap.
Array(size: Int, init: (Int) -> T)
HashMap(initialCapacity: Int, loadFactor: Float = 0.0f)
HashMap(initialCapacity: Int)
They are used to make a collection with specific size setting. You will NOT need them in usual situations.
// same as val arr = arrayOf(0, 2, 4, 8)
val arr = Array(4, index -> index * 2)
// map with initial capacity 8, which means that the map will prepare memory for 8 elements at the beginning.
val map = HashMap(8)
arrayOf and mapOf are functions that returns a new collection which has elements same as the parameters
inline fun <reified T> arrayOf(vararg elements: T): Array<T> (source)
fun <K, V> hashMapOf(vararg pairs: Pair<K, V>): HashMap<K, V> (source)
val arr1 = arrayOf(1, 2, 3, 4, 5, 6)
val arr2 = arrayOf("D", "E", "FG", "H")
val map1 = mapOf("a" to 2, "b" to 3)
val map2 = mapOf(4 to "SDF", 7 to "E", 8 to "T")
listOf and mapOf return some implementation of List and Map respectivelyKotlin collections have three types List, Set and Map. These collections are mutable and immutable. Maps are Hashmap which is key-value pairs.
The following are definitions from Kotlin documentation:
List:
A generic ordered collection of elements. Methods in this interface support only read-only access to the list; read/write access are supported through the MutableList interface.
val numbers: MutableList<Int> = mutableListOf(1, 2, 3)
Set:
A generic unordered collection of elements that does not support duplicate elements. Methods in this interface support only read-only access to the set; read/write access is supported through the MutableSet interface.
val animals = mutableSetOf("Lion", "Dog", "Cat")
Map:
A collection that holds pairs of objects (keys and values) and supports efficiently retrieving the value corresponding to each key. Map keys are unique; the map holds only one value for each key. Methods in this interface support only read-only access to the map; read-write access is supported through the MutableMap interface.
val populations = mutableMapOf(
Pair("Toronto", 3000),
Pair("Windsor", 400)
)
Now the difference between mutableListOf, mutableSetOf and mutableMapOf, and listOf, setOf and mapOf is changeability. You can add value(s) to collections which have mutable keyword in front and you cannot add values(s) to collections which do not have mutable keyword in front.
The following are examples of immutable collections:
var nums = listOf(1, 2, 3, 4)
var names = setOf("Bob", "John", "Elizabeth")
var population = mapOf(
Pair("Windsor", 2000),
Pair("Toronto", 50000)
)
HashMap:
HashMap is a Kotlin class of collection based on MutableMap interface. It stores the data in the form of key-value pairs.
val hashMap:HashMap<Int,String> = HashMap<Int,String>()
The Array
represents an array (specifically, a Java array when targeting the JVM platform) * Array instances can be created using the [arrayOf], [arrayOfNulls] and [emptyArray]
.
Reference: https://kotlinlang.org