Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

hashset
none
kotlin
communitycreator

What is the none method of the HashSet in Kotlin?

Programming Bytes

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.

Overview

In Kotlin, the none method of HashSet is used to check two separate operations:

  1. To check if a Set is empty.
  2. To check if no element in a Set matches the given condition.

Syntax

This method has two syntaxes.

  1. To check if the 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.


  1. Check if no element in the Set matches the given condition:
fun <T> Iterable<T>.none(predicate: (T) -> Boolean): Boolean
  • Arguments: This method takes a predicate functionPredicate is a functional interface that takes one argument and returns either true or false based on the condition defined. as an argument.

  • Return value: This method returns true if no element in the Set matches the given condition.

Example

The code below demonstrates how to use the none method.

fun main() {
//create a new HashSet which can have integer type as elements
var set: HashSet<Int> = hashSetOf<Int>()
println("\nThe set is : $set")
// use none method to check if the set is empty
println("set.none : ${set.none()}")
// add two entries
set.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}")
}
Using none method

Explanation

  • 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

hashset
none
kotlin
communitycreator

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.

Keep Exploring