Kotlin Tip #44: Implement collection plus and minus operators for adding or removing elements — 100 Kotlin Tips in 100 Days

Raphael De Lio
Kotlin with Raphael De Lio
3 min readJun 5, 2024

Twitter | LinkedIn | YouTube | Instagram
Tip #43: Use listOfNotNull for filtering null values from collections

In Kotlin, working with collections is a fundamental part of developing robust applications. To make these operations even more intuitive, Kotlin provides special operators: the plus (+) and minus (-) operators.

These operators simplify the process of adding or removing elements from collections, aligning with Kotlin's philosophy of writing concise, readable code.

The plus operator (+) allows you to add elements to a collection seamlessly. Unlike the mutable collection methods that modify the original collection, the + operator returns a new collection with the elements added, leaving the original collection unchanged. This approach is particularly useful for working with immutable collections or when you wish to avoid side effects.

Here’s how you can use the plus operator:

This operator can also be used to concatenate two collections:

val listOne = listOf("Kotlin", "Java")
val listTwo = listOf("Scala", "Groovy")
val combinedList = listOne + listTwo
println(combinedList) // Outputs: [Kotlin, Java, Scala, Groovy]

Removing Elements with Minus Operator

Conversely, the minus operator (-) provides a straightforward way to remove elements from a collection. Similar to the plus operator, it returns a new collection with the specified elements removed, ensuring the original collection remains intact.

Here’s how to utilize the minus operator:

val originalList = listOf("Kotlin", "Java", "Scala")
val reducedList = originalList - "Scala"
println(reducedList) // Outputs: [Kotlin, Java]

And like the plus operator, you can remove multiple elements or another collection entirely:

val listOne = listOf("Kotlin", "Java", "Scala", "Groovy")
val listTwo = listOf("Scala", "Groovy")
val prunedList = listOne - listTwo
println(prunedList) // Outputs: [Kotlin, Java]

Best Practices

When working with plus and minus operators, keep in mind that they return new collections. This behavior is beneficial for maintaining immutability but can lead to performance considerations if used extensively in large collections or within tight loops.

For mutable collections or performance-critical sections, consider using appropriate methods that modify the collection in-place.

By integrating these operators into your Kotlin code, you can maintain clean, readable, and efficient collection manipulation routines, adhering to Kotlin’s principles of concise and expressive coding.

Remember, while Kotlin aims to simplify common tasks, always weigh the readability and performance implications of your chosen approach to ensure it aligns with your application’s requirements.

I hope you have enjoyed this tip of our series! Don’t forget to subscribe and stay tuned for more Kotlin tips!

Stay curious!

Tip #45: Utilize value classes to avoid runtime overhead when wrapping values

Contribute

Writing takes time and effort. I love writing and sharing knowledge, but I also have bills to pay. If you like my work, please, consider donating through Buy Me a Coffee: https://www.buymeacoffee.com/RaphaelDeLio

Or by sending me BitCoin: 1HjG7pmghg3Z8RATH4aiUWr156BGafJ6Zw

Follow Me on Social Media

Stay connected and dive deeper into the world of Kotlin with me! Follow my journey across all major social platforms for exclusive content, tips, and discussions.

Twitter | LinkedIn | YouTube | Instagram

--

--