How to Convert Array to Set (HashSet) in Kotlin?

Related searches

I have an array of String

val array = arrayOf("a", "b", "c")

I need to convert it to HashSet

val set = HashSet<String>()

Use extension function toHashSet as follow

array.toHashSet()

That function belongs to Kotlin Library

/**
 * Returns a [HashSet] of all elements.
 */
public fun <T> Array<out T>.toHashSet(): HashSet<T> {
    return toCollection(HashSet<T>(mapCapacity(size)))
}

How to Convert Array to Set (HashSet) in Kotlin?, toHashSet. Common. JVM. JS. Native. 1.0. fun <T> Array<out T>.toHashSet(): HashSet<T>. fun ByteArray.toHashSet(): HashSet<Byte>. fun ShortArray. toHashSet()� These are part of Kotlin's standard library. The asterisk * is the spread operator. It applies all elements in a collection individually, each passed in order to a vararg method parameter. val array = arrayOf("data", "foo") // Multiple spreads ["data", "foo", "bar", "data", "foo"] val mySet = setOf(*array, "bar", *array)

Convert Array to Set

import java.util.*

fun main(args: Array<String>) {

    val array = arrayOf("a", "b", "c")
    val set = HashSet(Arrays.asList(*array))

    println("Set: $set")

}

When you run the program, the output will be:

Set: [a, b, c]

toHashSet, Assume that the array contains all distinct elements, otherwise all duplicates would be In Kotlin, the standard way to convert the specified array to a Set is with toSet() val set: MutableSet<T> = HashSet(). for (i in array) set.add(i). return set. This article explores different ways to convert an array to a Set using Kotlin. Assume that the array contains all distinct elements, otherwise all duplicates would be silently discarded. 1. toSet () function In Kotlin, the standard way to convert the specified array to a Set is with toSet () or toMutableSet () function.

Kotlin Collections Solution

Directly use toSet(*array) or toHashSet(*array). These are part of Kotlin's standard library.

The asterisk * is the spread operator. It applies all elements in a collection individually, each passed in order to a vararg method parameter.

val array = arrayOf("data", "foo")

// Multiple spreads ["data", "foo", "bar", "data", "foo"]
val mySet = setOf(*array, "bar", *array)

Passing no parameters setOf() results in an empty set.

These are all of the specific hash types you can use:

setOf()
hashSetOf()
linkedSetOf()
mutableSetOf()
sortableSetOf()

This is how to define the collection item type explicitly.

setOf<String>()
hashSetOf<MyClass>()

Convert an Array to a Set in Kotlin, Kotlin Program to Convert Array to Set HashSet and Vice-Versa:The collection object has a constructor that accepts a Collection object to initial� To convert array to set, we first convert it to a list using asList () as HashSet accepts list as a constructor. Then, we initialize set with the elements of the converted list. Example 2: Convert Array to Set using stream

Kotlin Program to Convert Array to Set HashSet and Vice-Versa , How to Convert between an Array and a Set Using Plain Java, Guava or Apache Set<Integer> targetSet = new HashSet<Integer>(Arrays. Kotlin Example. Convert Array to Set (HashSet) and Vice-Versa. Kotlin Example. Add Two Matrix Using Multi-dimensional Arrays. Join our newsletter for the latest updates.

Converting between an Array and a Set in Java, kotlin-stdlib / kotlin.collections / HashSet. Returns this array if it's not empty or the result of calling defaultValue function if the array Returns a set

Kotlin for Server Side. HashSet < E >, Returns this array if it's not empty or the result of calling defaultValue function if the array is empty.