0

What is a difference among different way of collections declaration?

I want to know the difference between:

  • array and arrayOf
  • hashmap and hashmapOf

enter image description here

4
  • Please explain your question further. Commented Jun 27, 2018 at 0:44
  • imean what relationship between array and arrayof ,hashmap and hashmap of Commented Jun 27, 2018 at 0:49
  • thanks you very much .i mean what purpose use it .are anthor way to write Arraylist and array Commented Jun 27, 2018 at 1:20
  • This is not a duplicate: as I understand it, the OP is asking, "why do we have both Array and ArrayOf"? The linked "duplicate" is explaining differences between Array, List, MutableList, etc. Commented Oct 29, 2018 at 4:07

2 Answers 2

2

Array 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")
Sign up to request clarification or add additional context in comments.

1 Comment

I would add that listOf and mapOf return some implementation of List and Map respectively
0

Kotlin 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

Comments