Kotlin program to remove all negative numbers from a list

Different ways in Kotlin to remove all negative numbers from a list:

In this tutorial, we will learn how to remove all negative numbers from a mutable list. For example, if the list is 1,2,3,4,5,-6,-7,-8,9, it will remove all negative numbers and convert it to 1,2,3,4,5,9.

We will learn different ways to solve this problem and mainly using Kotlin functions, not Java functions.

Method 1: Using filter(predicate) :

filter method takes one predicate and returns one new list matching the given predicate. We can use it to filter out all negative numbers from a list.

For example :

fun main() {
    val givenList = mutableListOf(1, 2, 3, 4, 5, 6, 7, -8, -9, -10)
    val newList = givenList.filter { it > 0 }

    print(newList)
}

It will print the below output :

[1, 2, 3, 4, 5, 6, 7]

Method 2: Using mapNotNull(transform) :

mapNotNull takes one transform function and applies the transform function on the list and returns a new list containing only the non-null values.

fun main() {
    val givenList = mutableListOf(1, 2, 3, 4, 5, 6, 7, -8, -9, -10)
    val newList = givenList.mapNotNull { if (it < 0) null else it }

    print(newList)
}

Here, if the element is less than 0, it converts that value to null and removes that value from the returned list.

Method 3: removeIf(predicate) :

removeIf takes one predicate and removes all elements from the list that satisfies the given predicate. It may throw NullPointerException or UnsupportedOperationException if the specified field is null or if elements can’t be removed from the collection.

The below example illustrates how it works :

fun main() {
    val givenList = mutableListOf(1, 2, 3, 4, 5, 6, 7, -8, -9, -10)
    givenList.removeIf { it < 0 }

    print(givenList)
}

It will print :

[1, 2, 3, 4, 5, 6, 7]

It modifies the original array.

Method 4: removeAll(predicate) :

removeAll removes all elements from a list that matches a given predicate. It returns one boolean value. true if any element was removed from the list and false otherwise.

Let’s take a look at an example :

fun main() {
    val givenList = mutableListOf(1, 2, 3, 4, 5, 6, 7, -8, -9, -10)
    givenList.removeAll { it < 0 }

    print(givenList)
}

It will print the below output :

[1, 2, 3, 4, 5, 6, 7]

Method 5: retainAll(predicate) :

retainAll retain all elements of a list that matches a given predicate :

fun main() {
    val givenList = mutableListOf(1, 2, 3, 4, 5, 6, 7, -8, -9, -10)
    givenList.retainAll { it > 0 }

    print(givenList)
}

Output :

[1, 2, 3, 4, 5, 6, 7]

Method 6: Using a loop :

Using any loop, we can iterate through the list and append the positive items to a new list :

fun main() {
    val givenList = mutableListOf(1, 2, 3, 4, 5, 6, 7, -8, -9, -10)
    val newList = mutableListOf<int>()
    givenList.forEach {
        if (it > 0) {
            newList.add(it)
        }
    }
    print(newList)
}

Similar tutorials :

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Below is a video that I did recently. If you love this please do subscribe to support us 😊❤️❤️