File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 1+ fun main () {
2+
3+ /*
4+ HashSet is a generic unordered collection of elements and it does not contain duplicate elements.
5+
6+ It implements the set interface. hashSetOf() is a function which returns a mutable hashSet,
7+ which can be both read and written.
8+
9+ The HashSet class store all the elements using hashing mechanism.
10+ */
11+
12+
13+ // declaring a hashSet of chars
14+ val chars = hashSetOf(' p' ,' a' ,' z' ,' w' )
15+
16+ // println set contains
17+ println (chars)
18+
19+ // You can add elements in a hashset using add() and addAll() functions.
20+ chars.add(' c' )
21+
22+
23+ val newChars = setOf (' b' ,' o' )
24+
25+ // making an extra set to add it in chars
26+ chars.addAll(newChars)
27+
28+ println (chars)
29+
30+ /*
31+ HashSet there is a hash value calculated for each object and this hash value determines the array index of the
32+ particular object in the container. So the order of inserted elements are naturally not preserved.
33+ This allows for accessing desired elements with O(1) complexity but it costs a lot of memory
34+
35+ For more : https://en.wikipedia.org/wiki/Hash_table
36+
37+ */
38+
39+ // removing a from the set
40+ chars.remove(' a' )
41+ }
You can’t perform that action at this time.
0 commit comments