Kotlin example for Beginners – Kotlin Program to Convert Array to Set (HashSet) and Vice-Versa

Kotlin Program to Convert Array to Set (HashSet) and Vice-Versa

In this program, you’ll learn to convert an array to a set and vice versa in Kotlin.

Example 1: 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]

In the above program, we’ve an array named array. 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 Set to Array


import java.util.*

fun main(args: Array<String>) {

    val set = HashSet<String>()
    set.add("a")
    set.add("b")
    set.add("c")

    val array = arrayOfNulls<String>(set.size)
    set.toArray(array)

    println("Array: ${Arrays.toString(array)}")

}

When you run the program, the output will be:

Array: [a, b, c]

In the above program, we’ve a HashSet named set. To convert set into an array, we first create an array of length equal to the size of the set and use toArray() method.

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.