polarla.blogg.se

Kotlin linkedlist
Kotlin linkedlist




Alternatively, we can use the indexing operator. We can use the put method to replace an existing element in the LinkedHashMap. Let us say we want to replace an element in the LinkedHashMap. Process finished with exit code 0 Replace element in LinkedHashMap - put function Println(key.toString() + " = " + planets) In the following example, we used the indexing operator to insert Planets into our LinkedHashMap. Alternatively we can use the indexing operator.

kotlin linkedlist kotlin linkedlist

We can use put method, that takes a key and value as parameters. In the following example, we created a LinkedHashMap. Process finished with exit code 0 LinkedHashMap put function Println("Four = " + planets.getOrDefault(4, "Alien Planet")) Var planets = linkedMapOf( 1 to "Earth", 2 to "Mars", 3 to "Jupiter") In the following example, we used the getOrDefault function to get the value of the planet, which does not exist. We can use getOrDefault method to get value for given key, and to return a default value, if the key is not present. Process finished with exit code 0 LinkedHashMap getOrDefault function In the following example, we are iterating through the LinkedHashMap of alphabets and printing each key and value for the alphabet. We can iterate a LinkedHashMap using for each loop. Process finished with exit code 0 Iterating a LinkedHashMap Val alphabetMap = linkedMapOf("A" to "Apple", "B" to "Ball", In the following example, we are creating a LinkedHashMap of alphabets.

kotlin linkedlist kotlin linkedlist

LinkedHashMap is mutable, meaning we are able to modify the Map once it is created. You can query the LinkedHashMap with a key, and get the value stored in it. Note that the insertion order is retained, even when you overwrite an existing value for a key. This is useful, when you would like the elements to retain the order of insertion. However, the LinkedHashMap maintains insertion order for the keys. A LinkedHashMap in Kotlin implements a key value pair dictionary similar to a HashMap, it in facts extends the HashMap class.






Kotlin linkedlist