
Ktolin contains a lot of collections and arrays , also a lot of helper functions to create or initialize collections or arrays.
collection is a set of objects of the same type. Objects or items in a collection are called elements or items.
Array also can defined as list of elements of same type .
in Kotlin Standard Library collections and Arrays has many interfaces and classes.
lets take a look at collection API

this diagram must contain : ArrayList
ArrayList can be consider as a Mutable version of List class
now we have Collections , Array and the last one is Arrays . Arrays is part of standard library and contains primitive types of array (intArray , shortArray ,etc)
Array Class
Array class is Immutable that means once array created and assign element
we can assign new elements because array has fixed size.
array only contain elements of one data type and must be initalized
Array<T>
T is a data type you must specify in array creation
let see the example:
//Array Constructor must accept array length length and init function which is a
//lambda expression
var numbers:Array<Int> = Array(9){i->i} //lenght 9
//lenght 10
var names:Array<String> = Array(10 ){i->
if (i==0){"abdo"
}
else if (i==1){
"Mona"
} else if (i==2){
"Kamal"
}
else if (i==3){
"Dalia"
}
else if (i==4){
"Ameer"
}
else if (i==8){
"Kamil"
}
else{
"Kururu"
}
}
Array constructor must accept an array length and init function
you can also create array as follow:
var foods:Array<String> = arrayOf("dsfsd" ,"adsfds")
this will create array with length of 2
because we use helper function arrayOf , this will initialize array and add elements without specify the size as we did in the first one
but you can’t add more elements , you get it?
another helper function to create array is by using helper function called arrayOfNulls and this will create an array with specific type and length and assign values to null
var foods = arrayOfNulls<Int>( 9)
for (f in foods){
println(f?:"no value") // elvis operator ?:
//when f is null return : no value
//to avoid NullPointerException
//because arrayOfNull could return Null Value
}
Other type of Arrays

koltin standard library also contains Arrays , which contains classes that represent arrays of primitive types without boxing overhead: ByteArray
, ShortArray
, IntArray
, and so on
lets create an array of type IntArray
var answers:IntArray = intArrayOf(2,23,23,2,3,23,)
we use helper intArrayOf to initialize array elements
remember array is fixed size , once created and initialized you can’t add more elements:)
for other primitives you can you can use their helpers : doubleArrayOf ,floatArrayOf , shortArrayOf , longArrayOf , etc
you can do many operations over arrays like: add element to specific index , find element with index ,sorting , find smallest or biggest element , etc.
Collections

Collections is Part Of Standard Library and contains Lists ,Set and Maps.
List is an ordered collection with access to elements by indices (indexes)— integer numbers that reflect their position. Elements can occur more than once in a list. An example of a list is a telephone number: it’s a group of digits, their order is important, and they can repeat.
Set is a collection of unique elements. It reflects the mathematical abstraction of set: a group of objects without repetitions(unique elements). Generally, the order of set elements has no significance. For example, the numbers on lottery tickets form a set: they are unique, and their order is not important.
Map (or dictionary) is a set of key-value pairs. Keys are unique, and each of them maps to exactly one value. The values can be duplicates. Maps are useful for storing logical connections between objects, for example, an employee’s ID and their position
Collection types:
collection can be classified to two groups :
Immutable: collections that accept no elements after initializaion
you can call it: read-only
Mutable : you can add more elements to mutable collection after initialization
from Collection Diagram above:
Iterable interface is the root of all collections (mutable or immutable)
MutableIterable is the root of all mutable Collections
Collection is the root of the collection hierarchy. This interface represents the common behavior of a read-only collection: retrieving size, checking item membership, and so on
MutableCollection is a Collection
with write operations, such as add
and remove
.
Note:
helper functions to create immutable collections : listOf ,setOf and mapOf
helper functions to create mutable collections : mutableListOf ,mutableSetOF and mutableMapOf
var countries:List<String> = listOf("Sudan" ,"Kongo" ,"Egypt" ,"Morocco","Senegal" ,"Libya")
countries.add() //X you can't use add function it is immutable list
to use add function you can change type to MutableList
var countries:MutableList<String> = mutableListOf("Sudan" ,"Kongo" ,"Egypt" ,"Morocco","Senegal" ,"Libya")
countries.add("Tanzania") //no you can use add function
another type of mutable collection is ArrayList :
Kotlin ArrayList class is used to create a dynamic array. Which means the size of ArrayList class can be increased or decreased according to requirement. ArrayList class provides both read and write functionalities.
var countries:ArrayList<String> = ArrayList(9) // not fixed size , 9 is just init size
countries.add("Sudan")
countries.add("Ethiopia")
countries.add("Somalia")
//you can also use helper function to create an array list
var cities:ArrayList<String> = arrayListOf("Khartoum" ,"Cairo" ,"Cape Town")
let’s back to collections
Set:
set is a list of unique elements
var countries:Set<String> = setOf("Sudan" ,"Sudan","Kongo" ,"Kongo" ,"Egypt" ,"Morocco","Senegal" ,"Libya")
for (
country in countries
){
println(country)
}
outputs:
Sudan
Kongo
Egypt
Morocco
Senegal
Libya
Set will git rid of second “Sudan” and “Kongo” because it is only contains unique items.
the mutable version of set if MutableSet
The default implementation of MutableSet is LinkedHashSet
this can preserve item orders so you can use functions like: first , last ,etc
fun main() {
var names :MutableSet<String> = hashSetOf("Kururu" ,"Aisha" ,"Zikra")
println(names.first()) // return : Kururu
println(names.last()) // reurn : Zikra
}
Map:
key-value pairs (called entries in Kotlin ) collection , it is like a dictionary or JSON object .
Note:
Map<K, V>
is not an inheritor of theCollection
interface; however, it's a Kotlin collection type as well ,AMap
stores key-value pairs (or entries); keys are unique, but different keys can be paired with equal values
to create map you have to specify a key data type and value data type
Map<K, V> : K is key Data type normally String ,and V represent a Value Type.
using helper function mapOf :
mapOf( key1 to value1 ,…. , keyN to vlaueN)
to is a keyword and the value comes after it
fun main() {
var employees:Map<String, String> = mapOf(
"manger" to "Kururu" ,
"viceManager" to "Aisha" ,
"projectManager" to "Zikra"
)
print(employees.get("projectManager")) // get function accept the key
//to iterate through a map
// use mapVariableName.entries
for (employee in employees.entries){
println(
employee.key + " "+
employee.value)
}
}
Collection or Array Of Objects:
you can also use collections and arrays to store object rather than simple data types(apart from Arrays which contains arrays of primitives so you can’t use it.
full example:
import java.util.UUID
fun main() {
//array class
var usersArray = Array<User>(2){i->User(name = "Kururu")}
//Arrays contains arrays of primitives so you can't use it
//List
var userList:List<User> = listOf(User(name = "Kururu"))
//ArrayList
var userArrayList:List<User> = arrayListOf(User(name = "Kururu"))
var userMutableList:MutableList<User> = mutableListOf(User(name = "Kururu")) // you can also use arrayListOf here because ArrayList is subtype of MutableList
//set
var userSets:Set<User> = setOf(
User(
uid = "1334",
name = "Abdo") ,
User(
uid = "5445",
name = "Abdo"),
User(
uid = "1334",
name = "Abdo") ,
)
for (user in userSets){
println(user.name)
}
//Map
var userMaps :Map<String, User> = mapOf(
"AndroidDev" to User(
uid = "5445",
name = "Abdo")
,
"UX" to
User(
uid = "40958645",
name = "Tebyan")
)
}
data class User(val uid:String =UUID.randomUUID().toString() ,
var name:String? = null
)
just to recap , keep in your mind:
Collections .
Array class and Arrays /
ArrayList(you can consider it as part of collection API and mutable version for List)
off course , there are many operations over collections include order , sort , group ,transformations , map…etc.
thanks for reading:)
support me by clapping :)
references:
https://kotlinlang.org/docs/arrays.html
https://kotlinlang.org/docs/collections-overview.html#collection-types