Kotlin Sequence toHashSet Function

The toHashSet function in Kotlin is used to convert a sequence into a HashSet. It is part of the Kotlin standard library and provides a way to transform a sequence into an unordered collection of unique elements.

Table of Contents

  1. Introduction
  2. toHashSet Function Syntax
  3. Understanding toHashSet
  4. Examples
    • Basic Usage
    • Converting a Sequence of Strings to a HashSet
    • Using toHashSet with Custom Objects
    • Chaining toHashSet with Other Functions
  5. Real-World Use Case
  6. Conclusion

Introduction

The toHashSet function allows you to convert a sequence into a HashSet, which is an unordered collection that contains no duplicate elements. This is useful for scenarios where you need a collection of unique elements and do not require a specific order.

toHashSet Function Syntax

The syntax for the toHashSet function is as follows:

fun <T> Sequence<T>.toHashSet(): HashSet<T> 

Returns:

  • A HashSet containing all unique elements from the sequence.

Understanding toHashSet

The toHashSet function works by iterating through the sequence and adding each element to a HashSet. Since HashSet does not allow duplicate elements, only unique elements from the sequence are added to the resulting set.

Examples

Basic Usage

To demonstrate the basic usage of toHashSet, we will create a sequence of integers and convert it into a HashSet.

Example

fun main() { val numbers = sequenceOf(1, 2, 3, 2, 1, 4, 5) val hashSet = numbers.toHashSet() println(hashSet) // Output: [1, 2, 3, 4, 5] } 

Output:

[1, 2, 3, 4, 5] 

Converting a Sequence of Strings to a HashSet

This example shows how to convert a sequence of strings into a HashSet.

Example

fun main() { val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Arjun", "Esha") val nameSet = names.toHashSet() println(nameSet) // Output: [Arjun, Bhaskar, Chitra, Esha] } 

Output:

[Arjun, Bhaskar, Chitra, Esha] 

Using toHashSet with Custom Objects

You can use the toHashSet function to convert a sequence of custom objects into a HashSet.

Example

data class Person(val name: String, val age: Int) fun main() { val people = sequenceOf( Person("Arjun", 25), Person("Bhaskar", 30), Person("Chitra", 22), Person("Arjun", 25), Person("Esha", 26) ) val peopleSet = people.toHashSet() println(peopleSet) // Output: [Person(name=Arjun, age=25), Person(name=Bhaskar, age=30), Person(name=Chitra, age=22), Person(name=Esha, age=26)] } 

Output:

[Person(name=Arjun, age=25), Person(name=Bhaskar, age=30), Person(name=Chitra, age=22), Person(name=Esha, age=26)] 

Chaining toHashSet with Other Functions

The toHashSet function can be chained with other sequence functions to perform more complex operations before converting the sequence into a HashSet.

Example

fun main() { val numbers = sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val evenNumberSet = numbers.filter { it % 2 == 0 } .toHashSet() println(evenNumberSet) // Output: [2, 4, 6, 8, 10] } 

Output:

[2, 4, 6, 8, 10] 

Real-World Use Case

Converting a Sequence of Products to a HashSet

In real-world applications, the toHashSet function can be used to convert a sequence of products into a HashSet to ensure that there are no duplicate products in the collection.

Example

data class Product(val name: String, val price: Double) fun main() { val products = sequenceOf( Product("Laptop", 1000.0), Product("Smartphone", 600.0), Product("Tablet", 300.0), Product("Smartphone", 600.0) ) val productSet = products.toHashSet() println(productSet) // Output: [Product(name=Laptop, price=1000.0), Product(name=Smartphone, price=600.0), Product(name=Tablet, price=300.0)] } 

Output:

[Product(name=Laptop, price=1000.0), Product(name=Smartphone, price=600.0), Product(name=Tablet, price=300.0)] 

Conclusion

The toHashSet function in Kotlin provides used for converting a sequence into a HashSet, ensuring that only unique elements are included in the resulting set. By understanding and using the toHashSet function, you can efficiently manage and process data in your Kotlin applications, ensuring that collections of elements contain no duplicates according to your requirements.

Leave a Comment

Scroll to Top