Kotlin LinkedHashMap
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
A LinkedHashMap in Kotlin implements a key value pair dictionary similar to a HashMap, it in facts extends the HashMap class. However, the LinkedHashMap maintains insertion order for the keys. This is useful, when you would like the elements to retain the order of insertion. Note that the insertion order is retained, even when you overwrite an existing value for a key.
You can query the LinkedHashMap with a key, and get the value stored in it. A LinkedHashMap is unsynchronized. It permits null as a key. LinkedHashMap is mutable, meaning we are able to modify the Map once it is created.
Initializing a LinkedHashMap in Kotlin
In the following example, we are creating a LinkedHashMap of alphabets. We used the helper function linkedMapOf.
package com.cosmiclearn.kotlin.linkedhashmap
fun main(args: Array<String>)
{
val alphabetMap = linkedMapOf("A" to "Apple", "B" to "Ball",
"C" to "Cat")
println(alphabetMap)
}
Output:
{A=Apple, B=Ball, C=Cat}
Process finished with exit code 0
We can iterate a LinkedHashMap using for each loop. In the following example, we are iterating through the LinkedHashMap of alphabets and printing each key and value for the alphabet.
package com.cosmiclearn.kotlin.linkedhashmap
fun main(args: Array<String>)
{
val alphabetMap = linkedMapOf("A" to "Apple", "B" to "Ball",
"C" to "Cat")
for((alphabet,word) in alphabetMap)
{
println(alphabet + " for " + word)
}
}
Output:
A for Apple
B for Ball
C for Cat
Process finished with exit code 0
We can use getOrDefault method to get value for given key, and to return a default value, if the key is not present. In the following example, we used the getOrDefault function to get the value of the planet, which does not exist.
package com.cosmiclearn.kotlin.linkedhashmap
fun main(args: Array<String>)
{
var planets = linkedMapOf<Int, String>( 1 to "Earth", 2 to "Mars", 3 to "Jupiter")
println("Four = " + planets.getOrDefault(4, "Alien Planet"))
}
Output:
Four = Alien Planet
Process finished with exit code 0
In the following example, we created a LinkedHashMap. We can use put method, that takes a key and value as parameters. Alternatively we can use the indexing operator. In the following example, we used the indexing operator to insert Planets into our LinkedHashMap.
package com.cosmiclearn.kotlin.linkedhashmap
fun main(args: Array<String>)
{
var planets = LinkedHashMap<Int, String>()
planets[1] = "Earth";
planets[2] = "Mars";
planets[3] = "Pluto"
for(key in planets.keys)
{
println(key.toString() + " = " + planets[key])
}
}
Output:
1 = Earth
2 = Mars
3 = Pluto
Process finished with exit code 0
Let us say we want to replace an element in the LinkedHashMap. We can use the put method to replace an existing element in the LinkedHashMap. Alternatively, we can use the indexing operator. In the following example, we used the indexing operator to replace Mars with Jupiter.
package com.cosmiclearn.kotlin.linkedhashmap
fun main(args: Array<String>)
{
var planets = LinkedHashMap<Int, String>()
planets[1] = "Earth";
planets[2] = "Mars";
planets[3] = "Pluto"
println("First map = ")
for(key in planets.keys)
{
println(key.toString() + " = " + planets[key])
}
planets[2] = "Jupiter"
println("\nAfter replacing = ")
for(key in planets.keys)
{
println(key.toString() + " = " + planets[key])
}
}
Output:
First map =
1 = Earth
2 = Mars
3 = Pluto
After replacing =
1 = Earth
2 = Jupiter
3 = Pluto
Process finished with exit code 0
Instead of put function or indexing operator, we can also use replace function to replace value in the LinkedHashMap. In the following example, we used replace method to replace the value in the LinkedHashMap.
package com.cosmiclearn.kotlin.linkedhashmap
fun main(args: Array<String>)
{
var planets = LinkedHashMap<Int, String>()
planets.put(1, "Earth");
planets.put(2, "Mars");
planets.put(3, "Pluto")
println("First map = ")
for(key in planets.keys)
{
println(key.toString() + " = " + planets.get(key))
}
planets.replace(2, "Jupiter")
println("\nAfter replacing = ")
for(key in planets.keys)
{
println(key.toString() + " = " + planets.get(key))
}
}
Output:
First map =
1 = Earth
2 = Mars
3 = Pluto
After replacing =
1 = Earth
2 = Jupiter
3 = Pluto
Process finished with exit code 0
In the following example, we created a LinkedHashMap. We can use putIfAbsent method, to only insert the value if the key is absent. putIfAbsent function takes a key and value as parameters. In the following example, we used the putIfAbsent method to insert new Planets into our LinkedHashMap.
package com.cosmiclearn.kotlin.linkedhashmap
fun main(args: Array<String>)
{
var planets = LinkedHashMap<Int, String>()
planets[1] = "Earth"
planets[2] = "Mars"
planets[3] = "Pluto"
planets.putIfAbsent(3, "Bluto")
planets.putIfAbsent(4, "Saturn")
for(key in planets.keys)
{
println(key.toString() + " = " + planets[key])
}
}
Output:
1 = Earth
2 = Mars
3 = Pluto
4 = Saturn
Process finished with exit code 0
Instead of creating a LinkedHashMap and then populating it, Kotlin provides an inbuilt linkedMapOf, which can be used to create a LinkedHashMap and assign entries to the LinkedHashMap. In the following example, we used linkedMapOf to create a LinkedHashMap of planets.
package com.cosmiclearn.kotlin.linkedhashmap
fun main(args: Array<String>)
{
var planets = linkedMapOf<Int, String>(1 to "Earth", 2 to "Mars", 3 to "Pluto")
println("First map = ")
for(key in planets.keys)
{
println(key.toString() + " = " + planets[key])
}
planets.replace(2, "Jupiter")
println("\nAfter replacing = ")
for(key in planets.keys)
{
println(key.toString() + " = " + planets[key])
}
}
Output:
First map =
1 = Earth
2 = Mars
3 = Pluto
After replacing =
1 = Earth
2 = Jupiter
3 = Pluto
Process finished with exit code 0
We can remove an entry from the LinkedHashMap using remove function. In the following example, we used remove function to remove the Mars from planet linkedhashmap.
package com.cosmiclearn.kotlin.linkedhashmap
fun main(args: Array<String>)
{
var planets = LinkedHashMap<Int, String>()
planets.put(1, "Earth");
planets.put(2, "Mars");
planets.put(3, "Pluto")
println("First map = ")
for(key in planets.keys)
{
println(key.toString() + " = " + planets.get(key))
}
planets.remove(2)
println("\nAfter removing = ")
for(key in planets.keys)
{
println(key.toString() + " = " + planets.get(key))
}
}
Output:
First map =
1 = Earth
2 = Mars
3 = Pluto
After replacing =
1 = Earth
2 = Jupiter
3 = Pluto
Process finished with exit code 0
We can print keys for the LinkedHashMap using keys property. In the following example, we used keys property to get the keys and we iterated through the keys and print them.
package com.cosmiclearn.kotlin.linkedhashmap
fun main(args: Array<String>)
{
var planets = LinkedHashMap<String, String>()
planets["Earth"] = "Blue marble. Has Water. Brimming with life!";
planets["Mars"] = "Red Planet. Barren. Has no life.";
planets["Pluto"] = "It is not a Planet. It is actually a space station for Aliens."
var planetsKey = planets.keys
for(key in planetsKey)
{
println("Planet = $key")
}
}
Output:
Planet = Earth
Planet = Mars
Planet = Pluto
Process finished with exit code 0
We can print values for the LinkedHashMap using values function. In the following example, we used values function to get the values and we iterated through the values and print them.
package com.cosmiclearn.kotlin.linkedhashmap
fun main(args: Array<String>)
{
var planets = LinkedHashMap<String, String>()
planets["Earth"] = "Blue marble. Has Water. Brimming with life!";
planets["Mars"] = "Red Planet. Barren. Has no life.";
planets["Pluto"] = "It is not a Planet. It is actually a space station for Aliens."
var planetsValues = planets.values
for(value in planetsValues)
{
println("Planet Description = $value")
}
}
Output:
Planet Description = Blue marble. Has Water. Brimming with life!
Planet Description = Red Planet. Barren. Has no life.
Planet Description = It is not a Planet. It is actually a space station for Aliens.
Process finished with exit code 0
We can check if key is present in LinkedHashMap using containsKey function. In the following example, we used containsKey function to check if the Planet is present in the LinkedHashMap.
package com.cosmiclearn.kotlin.linkedhashmap
fun main(args: Array<String>)
{
var planets = LinkedHashMap<String, String>()
planets["Earth"] = "Blue marble. Has Water. Brimming with life!";
planets["Mars"] = "Red Planet. Barren. Has no life.";
planets["Pluto"] = "It is not a Planet. It is actually a space station for Aliens."
if(planets.containsKey("Pluto")) {
println("Pluto is a planet!")
println("Pluto Description " + planets["Pluto"])
}
else {
println("Pluto is NOT a Planet!")
}
if(planets.containsKey("Zupiter")) {
println("Zupiter is a planet!")
println("Zupiter Description " + planets["Zupiter"])
}
else {
println("Zupiter is NOT a Planet!")
}
}
Output:
Pluto is a planet!
Pluto Description It is not a Planet. It is actually a space station for Aliens.
Zupiter is NOT a Planet!
Process finished with exit code 0
We can check if value is present in LinkedHashMap using containsValue function. In the following example, we used containsValue function to check if the Planet is present in the LinkedHashMap.
package com.cosmiclearn.kotlin.linkedhashmap
fun main(args: Array<String>)
{
var planets = LinkedHashMap<String, String>()
planets["Earth"] = "Blue marble. Has Water. Brimming with life!";
planets["Mars"] = "Red Planet. Barren. Has no life.";
planets["Pluto"] = "It is not a Planet. It is actually a space station for Aliens."
if(planets.containsValue("Blue marble. Has Water. Brimming with life!")) {
println("Earth is present in this Solar System!")
}
else {
println("Earth is not present in this Solar System!")
}
if(planets.containsValue("Red Planet. Barren. Has no life.")) {
println("Mars is present in this Solar System!")
}
else {
println("Mars is not present in this Solar System!")
}
}
Output:
Earth is present in this Solar System!
Mars is present in this Solar System!
Process finished with exit code 0
We can find the count of the number of elements using the size function. In the following example, we used size function to get the count of number of elements in the LinkedHashMap.
package com.cosmiclearn.kotlin.linkedhashmap
fun main(args: Array<String>)
{
var planets = LinkedHashMap<String, String>()
planets["Earth"] = "Blue marble. Has Water. Brimming with life!";
planets["Mars"] = "Red Planet. Barren. Has no life.";
planets["Pluto"] = "It is not a Planet. It is actually a space station for Aliens."
var sizeOfLinkedHashMap = planets.size
println("Size of LinkedHashMap is $sizeOfLinkedHashMap")
}
Output:
Size of LinkedHashMap is 3
Process finished with exit code 0
We can clear all entries from LinkedHashMap using clear function. In the following example, we used clear function to remove all the entries from planet linkedhashmap.
package com.cosmiclearn.kotlin.linkedhashmap
fun main(args: Array<String>)
{
var planets = LinkedHashMap<String, String>()
planets["Earth"] = "Blue marble. Has Water. Brimming with life!";
planets["Mars"] = "Red Planet. Barren. Has no life.";
planets["Pluto"] = "It is not a Planet. It is actually a space station for Aliens."
planets.clear()
var sizeOfLinkedHashMap = planets.size
println("Size of LinkedHashMap is $sizeOfLinkedHashMap")
}
Output:
Size of LinkedHashMap is 0
Process finished with exit code 0