Kotlin Collections With Examples #2

Vefa Can Beytorun
Paycell Tech Team
Published in
4 min readJan 8, 2024

We examined List, one of the three main headings specific to Kotlin collections. In this article, we will be exploring the other two main headings, Set and Map. If you haven’t read the first article, you can access it from our Paycell Tech account. Happy reading!

2. Set’s Specific Operations

In Kotlin, a set is a collection type that represents a data structure containing unique elements. It ensures that each element is present in the set only once. This characteristic makes Sets particularly useful when dealing with distinct elements. Here’s an example:

In this example, even though the element 4 is added third times, the set will only contain unique elements. Sets in Kotlin do not maintain order, so the order of elements is not significant.

union

In the context of sets, the union operation refers to combining the elements of two sets to create a new set that contains all unique elements from both sets. In mathematical terms, if you have sets A and B, the union of A and B, denoted as A B, consists of all elements that are in A, or in B, or in both. Here’s an example:

Here’s one more example about union:

intersect

The intersect function returns the intersection of two sets. This function creates a new Set containing elements that are common to both sets. A ∩ B Here’s an example:

subtract

The subtract function performs the operation of subtracting one set from another. It removes elements from the left-hand set that are present in the right-hand set, creating a new set. Here’s an example:

3. Map’s Specific Operations

Map is a data structure containing key-value pairs. Each key represents a value. Keys are usually unique, meaning no two elements with the same key can be found in a map. Values ​​are associated with keys. Here’s an example:

filter

You can filter maps with the filter function as well as other collections. Here’s an example:

In this example, a Map named ages is defined, where keys are names and values are ages. The filter function is applied to the ages map, and it takes a lambda function as an argument.

Plus and minus operators

The plus operator (+) is used to add or merge entries from one map into another, creating a new map with the combined entries. Here’s an example:

! Pay attention to the output. The value of the b key took the value found in the second map collected.

The minus operator (-) is used to create a new map by removing entries with specific keys from an existing map. Here’s an example:

Add and update entries

You can use put() keyword for add any things in Map. When a new entry is put into, it is added so that it comes last when iterating the map. Here’s an example:

Also you can use putAll() keyword o add multiple entities.

Remove entries

You can use remove() keyword for remove any things in Map. When calling remove() , you can pass either a key or a whole key-value-pair. If you specify both the key and value, the element with this key will be removed only if its value matches the second argument.

You can also remove entries from a mutable map by their keys or values. To do this, call remove() on the map’s keys or values providing the key or the value of an entry. When called on values, remove() removes only the first entry with the given value.

Conclusion

We examined the Set and Map types of Kotlin collections. For Set, we covered the special operators union, intersect, and subtract functions with examples. In the case of the Map type, we demonstrated filtering maps with the filter function and combining or subtracting maps with addition/subtraction operators. Kotlin collections offer powerful and flexible operations to developers, and this article serves as a brief guide for those looking to understand Kotlin collections.

HAPPY NEW YEAR

References

--

--