Press enter to see results or esc to cancel.

# Lesson 48 – Set and MutableSet in Kotlin Programming Language.

()

The Set and MutableSet (set) classes allow you to store a set of elements that must be all different. It does not allow storing repeated data.

A set is a collection of items without a specific order, unlike lists.

Problem 1

Create a mutable set (MutableSet) with a series of Int values. Test the main properties and methods for managing the set.

Project 187

``````fun main (args: Array <String>) {
// www.coding180.com
val set1: MutableSet <Int> = mutableSetOf (2, 7, 20, 150, 3)
println ("Complete assembly list")
for (element in set1)
print ("\$element")
println ()
println ("Number of elements in the set: \${set1.size}")
println ("Complete listing after adding 500")
for (element in set1)
print ("\$element")
println ()
println ("Complete assembly listing after re-adding 500")
for (element in set1)
print ("\$element")
println ()
if (500 in set1)
println ("500 is stored in the set")
else
println ("500 is not stored in the set")
println ("We removed element 500 from the set")
set1.remove (500)
if (500 in set1)
println ("500 is stored in the set")
else
println ("500 is not stored in the set")
val cont = set1.count {it >= 10}
println ("Number of items with values ​​greater than or equal to 10: \$cont")
}``````

We create a MutableSet with elements of type Int and store 5 integers:

```    Val set1: MutableSet <Int> = mutableSetOf (2, 7, 20, 150, 3)
```

We can traverse with a for all elements of a set just like the other collections provided by Kotlin programming language:

```    For (element in set1)
Print ("\$element")
```

To add a new element to a set we call the add method (this can be done only with the MutableSet class and not with Set):

```    Set1.add (500)
```

If the element that we send to the add method already exists then it is not inserted (this because the collections of set type can not have repeated values)

To verify if an element is contained in the set we can do it with the operator in:

```    If (500 in set1)
Println ("500 is stored in the set")
Else
Println ("500 is not stored in the set")
```

To remove an element from the set we can call the remove method and pass the value to delete:

```    set1.remove (500)
```

If we want to know all the properties and methods of MutableSet we can visit the documentation of the standard library of Kotlin.

The same for Set we can visit the documentation of the standard Kotlin library .

Problem 1

Create an immutable set that stores the dates of this year that are holidays.
Then key in a date and check if it is in the set of holidays

Project188

``````data class Date (val day: Int, val month: Int, val year: Int)
fun main (args: Array <String>)
{ var holidays: Set <Date> = setOf (Date (1, 1, 2017), Date (25, 12, 2017))
println ("Enter a date")
print ("Enter the day:")
val day = readLine () !!. toInt ()
print ("Enter the month:")
val month = readLine () !!. toInt ()
print ("Enter year:")
val year = readLine () !!. toInt ()
if (Date (day, month, year) in holidays)
println ("The date entered is a holiday")
else
println ("The date entered is not a holiday")
}``````

We declare a data class that represents a date:

```Data class Date (val day: Int, val month: Int, val year: Int)
```

We define an immutable set of Date type and save two dates by calling the setOf  function:

```    Var holidays: Set <Date> = setOf (Date (1, 1, 2017),
Date (25, 12, 2017))
```

We load any date by keyboard:

```    Println ("Enter a date")
Print ("Enter the day:")
Val day = readLine () !!. ToInt ()
Print ("Enter the month:")
Val month = readLine () !!. ToInt ()
Print ("Enter year:")
Val year = readLine () !!. ToInt ()
```

Using the in operator we verify if the entered date is in the set of holidays:

```    If (Date (day, month, year) in holidays)
Println ("The date entered is a holiday")
Else
Println ("The date entered is not a holiday")
```

Proposed problem

• Define a MutableSet and store 6 random values between 1 and 50. The MutableSet type object represents a lottery card.
Start to generate random values (between 1 and 5) all different and stop when all the cards contained in the lottery come out. Show how many numbers had to be dealt with until the card was completed.
`Solution`
``````fun generateC (carton: MutableSet <Int>) {
do {
val value = ((Math.random () * 50) + 1) .toInt ()
} while (carton.size != 6)
}

fun generateBill (bolillero: MutableSet <Int>) {
do {
val value = ((Math.random () * 50) + 1) .toInt ()
} while (bolillero.size != 50)
}

fun verifywinner (carton: MutableSet <Int>, bolillero: MutableSet <Int>) {
var hit = 0
var countbolts = 0
var attempts = 0
for (bolilla in bolillero) {
countbolts
if (bolilla in carton) {
hit
if (hit == 6) {
attempts = countbolts
break
}
}
}
println ("Attempts were made until the carton won.")
}

fun main (args: Array <String>) {
val carton: MutableSet <Int> = mutableSetOf ()
generateC (carton)
println ("Lottery card generated")
println (carton)
val bolillero: MutableSet <Int> = mutableSetOf ()
generateBill (bolillero)
println ("Number of bolillero")
println (bolillero)
verifywinner (carton, bolillero)
}``````

Click on a star to rate it!

Average rating / 5. Vote count:

Be the first to rate this post.!

As you found this post useful...