celebritywera.blogg.se

Arraylist in kotlin
Arraylist in kotlin






arraylist in kotlin

It preserves the iteration order of the elements as it uses LinkedHashSet implementation of a MutableSet.Return type of toMutableSet() is a MutableSet.This function will create a new MutableSet (which is a data structure that doesn't allow duplicate entries) from the given array. Node(id=4, fullyQualifiedName=) Using the toMutableSet() function It does not guarantee the preservation of the iteration order of the elements.Return type of toHashSet() is a HashSet.This function will create a new HashSet (which is a data structure that doesn't allow duplicate entries) from the given array. Node(id=4, fullyQualifiedName=) Using the toHashSet() function } Code Output Node(id=1, fullyQualifiedName=) It guarantees the preservation of the iteration order of the elements.Ĭode Sample data class Node(val id: Int, val fullyQualifiedName: String).This is the simplest and the best way to remove duplicates from an array in kotlin. This function will create a new Set (which is a data structure that doesn't allow duplicate entries) from the given array. You have a data class called Node: data class Node(val id: Int, val fullyQualifiedName: String)Īnd, an array of Nodes: val nodes = arrayOf( To understand the usage of each function let us consider the following example: There are a few ways to remove duplicates from an array in kotlin:








Arraylist in kotlin