0
0
Kotlinprogramming~10 mins

Java streams vs Kotlin sequences - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a Kotlin sequence from a list.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val seq = numbers.[1]()
Drag options to blanks, or click blank then click option'
AtoSequence
Bstream
CasSequence
DsequenceOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using Java's stream() method which is not available on Kotlin lists.
Trying to use toSequence() which does not exist.
2fill in blank
medium

Complete the code to filter even numbers using a Kotlin sequence.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val evens = numbers.asSequence().[1] { it % 2 == 0 }.toList()
Drag options to blanks, or click blank then click option'
AflatMap
Bmap
Creduce
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using map which transforms elements but does not filter.
Using reduce which combines elements into one value.
3fill in blank
hard

Fix the error in the code to convert a Kotlin sequence back to a list.

Kotlin
val seq = listOf(1, 2, 3).asSequence()
val list = seq.[1]()
Drag options to blanks, or click blank then click option'
AasList
BtoList
CtoSequence
DtoArray
Attempts:
3 left
💡 Hint
Common Mistakes
Using toSequence() which does not convert back to list.
Using asList() which is not a Kotlin function.
4fill in blank
hard

Fill both blanks to create a Java stream from a list and collect it back to a list.

Kotlin
val list = listOf(1, 2, 3, 4)
val stream = list.[1]()
val result = stream.collect(Collectors.[2]())
Drag options to blanks, or click blank then click option'
Astream
BtoList
DtoSet
Attempts:
3 left
💡 Hint
Common Mistakes
Using Kotlin sequence methods instead of Java stream methods.
Using toSet collector instead of toList.
5fill in blank
hard

Fill all three blanks to create a Kotlin sequence, filter odd numbers, and convert to a list.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val result = numbers.[1]().[2] { it % 2 != 0 }.[3]()
Drag options to blanks, or click blank then click option'
AasSequence
Bfilter
CtoList
Dmap
Attempts:
3 left
💡 Hint
Common Mistakes
Using map instead of filter for selecting odd numbers.
Forgetting to convert the sequence back to a list.