Grokking Modern System Design Interview for Engineers & Managers
Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.
In Kotlin, the none
method of HashSet
is used to check two separate operations:
Set
is empty.Set
matches the given condition.This method has two syntaxes.
Set
is empty:fun <T> Iterable<T>.none(): Boolean
Arguments: This method doesn’t take any argument.
Return value: This method returns true
if the Set
is empty.
Set
matches the given condition:fun <T> Iterable<T>.none(predicate: (T) -> Boolean): Boolean
Arguments: This method takes a
Return value: This method returns true
if no element in the Set
matches the given condition.
The code below demonstrates how to use the none
method.
fun main() {//create a new HashSet which can have integer type as elementsvar set: HashSet<Int> = hashSetOf<Int>()println("\nThe set is : $set")// use none method to check if the set is emptyprintln("set.none : ${set.none()}")// add two entriesset.add(1)set.add(2)println("\nThe set is : $set")var isAllKeysLessThanTwo = set.none( { it > 3 } )println("Checking if all elements of the set is less than 3 : ${isAllKeysLessThanTwo}")set.add(4)println("\nThe set is : $set")isAllKeysLessThanTwo = set.none( { it > 3 } )println("Checking if all elements of the set is less than 3 : ${isAllKeysLessThanTwo}")}
Line 3: We create a new HashSet
object with the name set
. We’ve used the hashSetOf
method to create an empty HashSet
.
Line 7: We use the none
method to check if the set
is empty. In our case, the set
is empty, so true
is returned.
Lines 9 and 10: We use the add()
method to add two new elements(_1,2_)
to the set
.
Line 13: We use the none
method with a predicate function. The predicate function returns true
if the element is greater than 3
. In our case, there is no element is greater than 3
. So, the predicate fails for all entries and it returns true
.
Line 16: We use the add()
method to add a new element 4
to the set
. Now the set
is [1, 2, 4]
.
Line 18: We use the none
method again with the same predicate function. There is one element 4
that is greater than 3
. The predicate returns true
for that element. Therefore, the none
method will return false
.
RELATED TAGS
CONTRIBUTOR
Grokking Modern System Design Interview for Engineers & Managers
Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.