Kotlin Collections

There are four type of standard collection are available in kotlin programming language which are capable to grouping elements. In this section view basic overview of collection like how to define and how to use there available standards methods.

Array

Array is collection of ordered elements. Before using array there are need to declaring its size. Because this size is decide, how many number of element will be stored by any particular array. When not provide initial size of array then in this situation compiler are provide the size of array by using the get initial number of elements. So we cannot create an empty array. There are many ways are possible to declare array in Kotlin programming.

Instance Method Data Type Example Total elements Default Value
ByteArray(size) Long var arrayElement=ByteArray(4) 4 0
IntArray(size) Int var arrayElement=IntArray(5) 5 0
ShortArray(size) Short var arrayElement=ShortArray(7) 7 0
LongArray(size) Long var arrayElement=LongArray(3) 3 0
FloatArray(size) Long var arrayElement=FloatArray(10) 10 0
DoubleArray(size) Long var arrayElement=DoubleArray(6) 6 0

In this table are view how to implement primitive data elements. For example.

fun main(args: Array<String>){
   //Byte Data type array
   var byteArray=ByteArray(3)
   println(byteArray.joinToString(separator=" "))
   
   //Int Data type array
   var intArray=IntArray(8)
   println(intArray.joinToString(separator=" "))
   
   //Short Data type array
   var shortArray=ShortArray(6)
   println(shortArray.joinToString(separator=" "))
  
   //Long Data type array
   var longArray=LongArray(10)
   println(longArray.joinToString(separator=" "))
  
   //Float Data type array
   var floatArray=FloatArray(6)
   println(floatArray.joinToString(separator=" "))
    
   //Double Data type array
   var doubleArray=DoubleArray(7)
   println(doubleArray.joinToString(separator=" "))
}
//Output
0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0.0 0.0 0.0 0.0 0.0 0.0
0.0 0.0 0.0 0.0 0.0 0.0 0.0

Array element are used (getting and set element values) by index value. First element of array are start with in at zero and next upcoming next index are increment one by one.

fun main(args: Array<String>){
    
   var intArray=IntArray(8) 
   
   //Set new values of array element by using index
   intArray[0]=10
   intArray[1]=35
   intArray[2]=95
   intArray[3]=70
   intArray[7]=12
   intArray[6]=13
   
   //Display specific array element
   println(intArray[7]) //index 7
   
   //Display all array element 
   println(intArray.joinToString(separator=" "))
}
Use and modification of Array Element
12
10 35 95 70 0 0 13 12

There are fixed number of array element will possible. They are cannot modified during program execution. Array variable are hold the instance reference of array. When In case we are need to add extra element to existing array in this situation first create new array and assign this new instance array to variable (similar data type array and dimension).

fun main(args: Array<String>){
    
       var first = IntArray(4);
       first[0]=100
       first[1]=10
       first[2]=20
       first[3]=30
    
       println("Before Array : "+first.toList())
       var newFirst : IntArray = IntArray(6);
       newFirst[0]=first[0]
       newFirst[1]=first[1]
       newFirst[2]=first[2]
       newFirst[3]=first[3]
       newFirst[4]=40; //new element
       newFirst[5]=50; //new element
      
       //Assign the reference of array instance
       first=newFirst
       
       println("After Array : "+first.toList())
}
Assign new instance reference of array
Before Array : [100, 10, 20, 30]
After Array : [100, 10, 20, 30, 40, 50]

That is simplest overview of array. Learn more about kotlin arrays.

List

List is collection of elements, That is similar to array but there are possible to add and remove element in list. Empty MutableList are define using of this syntax.

MutableList<DataType> = mutableListOf()
fun main(args: Array<String>){
    //Create empty list of integer element
    val dataList: MutableList<Int> = mutableListOf()
    println(dataList) // empty list []
    dataList.add(1)
    dataList.add(2)
    dataList.add(3)
    dataList.add(13)
  
    println(dataList) //[1, 2, 3, 13]
    
    println("After Remove element 3")
    dataList.remove(3)
    println(dataList) //1, 3, 13]
}
Add and remove kotlin list element
[]
[1, 2, 3, 13]
After Remove element 3
[1, 2, 13]

List element are retrieved by index values. And when removing specific element of list, after upcoming next element's index are change. Data type of this list is form of an integer values so this list is capable to store integer values. If set data type in form of Any Type then it will accept any primitive data element.

fun main(args: Array<String>){
    //Create empty list of Any Type
    val dataList: MutableList<Any> = mutableListOf()
    println(dataList) //[]
    
    dataList.add(2020)
    dataList.add(11.21)
    dataList.add(true)
    dataList.add("Kotlin")
    println(dataList) //[2020, 11.20, true, Kotlin]
}
Mutable List of any data type
[]
[2020, 11.21, true, Kotlin]

Set

Set is collection of unique elements.

fun main(args: Array<String>){
    // Create a mutable set
    val animals = mutableSetOf("Lion","Camel", "Dog", "Cat", "Elephant", "Python", "Chimpanzee")
    
    //iterating set elements
    for(element in animals){
        println(element)
    }
}
Example of Set elements
Camel
Python
Dog
Chimpanzee
Cat
Lion
Elephant

Map

Map is a collection of key and value pair. key are unique and each key are contains values. There is similar to Python Programming dictionary.

fun main(args: Array<String>){
    // Create a map 
    val population = mutableMapOf("India" to 1_368_297_504 ,
                                  "China" to 1_419_912_181 , 
                                 "United States" to  329_023_436,
                                 "Indonesia" to 269_453_619)
    
    //iterating map elements
    for((key,value) in population){
        println(key +" : "+value )
    }
}
Map Element Example In Kotlin
India : 1368297504
China : 1419912181
United States : 329023436
Indonesia : 269453619


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved