Updated April 10, 2023
Introduction to Kotlin collections
Kotlin collections are one of the standard library packages and it contains the number of objects that may be of the same and different type. the objects in the collections are named as elements or items. it has many types like list, set and map for each type has different ordered and un-ordered concept. It manipulates the collections independently accept and the exact type of the objects stored in the separate memory. It provides a rich set of tools to manage the collected data the mutable and immutable category supports both read and writeable data for performing the operation.
Syntax
The kotlin collections are one of the types and it is the covariant mode. It has a set of standard library implementations that contains basic types like sets, lists, and maps. A pair of interfaces represents the collection types that can be the read-only format.
fun main(args: Array<String>)
{
var vars= mapOf() // immutable collection method
var vars1 = mutableListOf() // mutable collection method
---some kotlin logic codes depends upon the requirement—
}
The above codes are the basic syntax for utilizing the collections in the kotlin language. It depends upon the requirement we may use the list, set and map, etc.
How do Collections work in Kotlin?
The kotlin language has many default packages among this collection is one of the important in the user end applications. It can have many default methods to store and retrieve the data from the backend to the front end. Like that List is a collection that can store the value with the index-based format also we can retrieve the data with the help of the index. Map is similar to the java language we can retrieve the store and retrieve the data with the help of key-value pairs. The key is the unique one and it does not accept the duplicate and null keys. But the value may be null and it accepts the duplicate. Kotlin Collections is the standard library and it usually contains the number of objects which is related to the same and different type and these objects in the collection package is named it as elements or items. But the element or item that we used in the application with collection concepts like list, map, or set the default methods are used so that we can split the collection into two different categories like mutable and immutable collections. If we use immutable collections it supports only read-only format and its functionalities so it can’t modify the elements or values. When we use a mutable one we can modify the elements.
Examples of Kotlin collections
Let us discuss examples of Kotlin collections.
Example #1
Code:
package one;
interface first {
val fr: Int
fun eg() : String
fun dem() {
println("Welcome To My Domain its the first example that related to the kotlin collections")
}
}
class second: first {
override val fr: Int = 99
override fun eg() = "Have a Nice day users"
}
interface third {
fun eg1() {
println("We have discussed about the kotlin collections on this example")
}
}
interface four {
fun eg2() {
println("Based on the requirement we used list, map and set interfaces")
}
}
fun main(args: Array<String>) {
val vars = listOf("siva","raman","arun", "fourth input strings", "fifth input strings")
for(x in vars){
println("Thank you users for spending the time with our application $x" )
}
}
Output:
In the above example we used kotlin class and interfaces additionally we can use immutable collections like listof() method to store the data. The immutable collections are not adding the values in the memory. It is a default and fixed one.
Example #2
Code:
package one;
fun main(args : Array<String>) {
println("Welcome to my domain its the second example that related to the kotlin collections")
var second = mutableListOf("first input strings","second input strings","third input strings","fourth input strings", "fifth input strings","sixth input strings","seventh input strings")
second[0] = "siva"
second[1] = "raman"
second[2] = "input strings"
second.add("raman")
second.add("eigth input strings")
second.add("ninth input strings")
for(x in second){
println(x)
}
var third = mutableMapOf<Int,String>(1 to "first input strings",2 to "second input strings",3 to "third input strings", 4 to "fourth input strings", 5 to "fifth input strings")
third.put(2,"arun")
third.put(3,"kumar")
third.put(4,"tenth input strings")
third.put(5,"eleventh input strings")
for(y in third.values){
println(y)
}
var four = mutableSetOf<Int>(6,10)
four.add(2)
four.add(3)
for(z in four){
println("Thank you users for to spending the time with our application $z")
}
}
Output:
In the second example we used mutable collection methods like mutableListof(), mutableSetof(), and mutableMapof() methods for accessing and operating the user data. With the help of mutableSetof() method, we can add the values additionally whenever we require. Like that mutableListof() method we can add the values in the memory list.
Example #3
Code:
package one;
fun main(args : Array<String>) {
println("Welcome to my domain its the second example that related to the kotlin collections")
var vars = setOf(3,6,9,12,"String","Ramsn")
for(x in vars){
println(x)
}
val p = setOf(3, 2, 5, 7)
println("If your input numbers are not null values: "+p.mapNotNull
{ if ( it == 1) null else it * 2 })
println("If your input numbers are not null values: "+p.mapIndexedNotNull
{ q, value -> if (q == 0) null else value * q })
val r: Set<Any> = setOf(0,1,2,3,4,5,"siva","raman")
val s = setOf(6,4,29)
println("Have a nice day users")
for(u in r){
println(u)
}
println("Your input datas are")
println(r.contains("siva"))
println("Your second input datas should be the number format")
println(r.contains(33))
println("Your third input datas should be the numeric characters")
println(r.containsAll(s))
val t: Set<Any> = setOf(5,10,15,20,25,30,"arun","kumar","sasi","jai")
println("Thank you users for spending the time with our application")
for(v in t){
println(v)
}
println("print t.elementAt(4)")
println(t.elementAt(3))
println("print t.elementAtOrNull(5)")
println(t.elementAtOrNull(5))
}
Output:
In the final example, we used the collection interface called set with the help of default methods like setof(), containAll(), and elementAt() by using the index-based search the value is stored and retrieved the data.
Conclusion
In kotlin language collection is the main and major role for the real-time web-based and mobile applications. The real-time data are stored and retrieved using some default methods to perform the user operations in both web and mobile-based applications. The end-user operates the data whenever they require the data will be fetched from the backend servers.
Recommended Articles
This is a guide to Kotlin collections. Here we discuss the introduction, syntax, and working of collections in Kotlin along with different examples and its code implementation. You may also have a look at the following articles to learn more –