Industrial Training




Kotlin HashSet class


Kotlin HashSet is class of collection which extends AbstractMutableSet class and implements Set interface. The HashSet class store elements using hashing mechanism. It support both read and write functionality. It does not support duplicate value and does not make guarantees about the order sequence of element.


HashSet class declaration


open class HashSet : AbstractMutableSet (source)  

Constructor of Kotlin HashSet class


Constructor Description
HashSet() It constructs an empty HashSet instance
HashSet(initialCapacity: Int, loadFactor: Float = 0f) It is used to constructs a HashSet of specified capacity.
HashSet(elements: Collection) It constructs a HashSet instance using elements of specified collection.

Functions of Kotlin HashSet class


Functions Description
open fun add(element: E): Boolean It adds the given element to the collection.
open operator fun contains(element: E): Boolean It checks the specified element is present in current collection
open fun isEmpty(): Boolean It checks the current collection is empty (not contain any element). If found collection is empty returns true otherwise false.
open fun iterator(): MutableIterator It returns an iterator over the elements of current object.
open fun remove(element: E): Boolean It removes the mention element if present in current collection. It returns true if it removes otherwise false.
open fun clear() It deletes all the elements from this collection.

Property of Kotlin HashSet class


Property Description
open val size: Int This property is used to return the size of HashSet collection.

Kotlin HashSet Example 1- capacity


Let's create an example of HashSet defining it capacity. Capacity defines the total number of element to be added in the HashSet. It can be increase of decrease later according to need.


fun main(args: Array){  
    var hashSet = HashSet(6)  
    hashSet.add(2)  
    hashSet.add(13)  
    hashSet.add(6)  
    hashSet.add(5)  
    hashSet.add(2)  
    hashSet.add(8)  
    println("......traversing hashSet......")  
    for (element in hashSet){  
        println(element)  
    }      
}  

Output:
......traversing hashSet......
8
2
13
5
6

Kotlin HashSet Example 2 - generic


For more specific we can provide the generic types of HashSet class using its method hashSetOf().


fun main(args: Array){  
    var hashSetOf1 = hashSetOf(2,13,6,5,2,8)  
    var hashSetOf2: HashSet = hashSetOf("Vijay","Ashu" ,"Vijay","Roshan")  
    println("......traversing hashSetOf1......")  
    for (element in hashSetOf1){  
        println(element)  
    }  
    println("......traversing hashSetOf2......")  
    for (element in hashSetOf2){  
        println(element)  
    }  
}  

Output:
......traversing hashSetOf1......
8
2
13
5
6
......traversing hashSetOf2......
Ashu
Roshan
Vijay

Kotlin HashSet Example 3 - add() and addAll()


The add() function is used to add the element in the HashSet instance whereas addAll() function add all the elements of specified collection to HashSet.


fun main(args: Array){  
    var hashSet = HashSet(3)  
    val intSet = setOf(6,4,29)  
    hashSet.add(2)  
    hashSet.add(13)  
    hashSet.add(6)  
    hashSet.add(5)  
    hashSet.add(2)  
    hashSet.add(8)  
    println("......traversing hashSet......")  
    for (element in hashSet){  
        println(element)  
    }  
    hashSet.addAll(intSet)  
    println("......traversing hashSet after hashSet.addAll(intSet)......")  
    for (element in hashSet){  
        println(element)  
    }  
}  


Output:
......traversing hashSet......
8
2
13
5
6
......traversing hashSet after hashSet.addAll(intSet)......
2
4
5
6
8
13
29

Kotlin HashSet Example 4 - size, contains() and containsAll()


The size property returns a total elements present in HashMap. The contains() function returns true if the mention element in it is contained in collection whereas containsAll() function checks all the elements of specified collection is contained in this collection.


fun main(args: Array){  
    var hashSetOf1: HashSet = hashSetOf(2,6,13,4,29,15)  
    val mySet = setOf(6,4,29)  
  
    println("......traversing hashSetOf1......")  
    for (element in hashSetOf1){  
        println(element)  
    }  
    println(".....hashSetOf1.size.....")  
    println(hashSetOf1.size)  
    println(".....hashSetOf1.contains(13).....")  
    println(hashSetOf1.contains(13))  
    println("....hashSetOf1.containsAll(mySet)...")  
    println(hashSetOf1.containsAll(mySet))  
}  


Output:
......traversing hashSetOf1......
2
4
13
29
6
15
.....hashSetOf1.size.....
6
.....hashSetOf1.contains(13).....
true
....hashSetOf1.containsAll(mySet)...
true

Kotlin HashSet Example 5 - remove() and removeAll()


The remove() function removes the specified element from the collection if it is present whereas removeAll() function removes all the specified elements from current collection if they are present.


fun main(args: Array){  
    var hashSetOf1: HashSet = hashSetOf(2,6,13,4,29,15)  
    val mySet = setOf(6,4,29)  
    
    println("......traversing hashSetOf1......")  
    for (element in hashSetOf1){  
        println(element)  
    }  
    println(".....hashSetOf1.remove(6)......")  
    println(hashSetOf1.remove(6))  
    println("......traversing hashSetOf1 after remove(6)......")  
    for (element in hashSetOf1){  
        println(element)  
    }  
    println("......hashSetOf1.removeAll(mySet)......")  
    println(hashSetOf1.removeAll(mySet))  
    println("......traversing hashSetOf1 after removeAll(mySet)......")  
    for (element in hashSetOf1){  
        println(element)  
    }  
}  

Output:
......traversing hashSetOf1......
2
4
13
29
6
15
.....hashSetOf1.remove(6)......
true
......traversing hashSetOf1 after remove(6)......
2
4
13
29
15
......hashSetOf1.removeAll(mySet)......
true
......traversing hashSetOf1 after removeAll(mySet)......
2
13
15

Kotlin HashSet Example 6 - isEmpty() and isNotEmpty()


The isEmpty() function checks the current collection is empty whereas isNotEmpty() function checks the current collection is not empty.


fun main(args: Array){  
    var hashSetOf1: HashSet = hashSetOf(2,6,13,4,29,15)  
      
    println("......traversing hashSetOf1......")  
    for (element in hashSetOf1){  
        println(element)  
    }  
    println(".....hashSetOf1.isEmpty()....")  
    if(hashSetOf1.isEmpty()){  
        println("hash set is empty")  
    }  
    else{  
        println("hash set is not empty")  
    }  
    println(".....hashSetOf1.isNotEmpty()....")  
    if(hashSetOf1.isNotEmpty()){  
        println("hash set is not empty")  
    }  
    else{  
        println("hash set is empty")  
    }  
}  


Output:
......traversing hashSetOf1......
2
4
13
29
6
15
.....hashSetOf1.isEmpty()....
hash set is not empty
.....hashSetOf1.isNotEmpty()....
hash set is not empty



Hi I am Pluto.