Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 536 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use cases of removeall and removeif

#1
I found this:

fun main() {
val list: MutableList<Int> = mutableListOf(1, 2, 3, 4, 5)

list.removeAll { x -> x in 1..3 } // [4, 5]
list.removeIf { x -> x in 1..3 } // [4, 5]
}

Both of them yield the same result.

Though I understand that `removeAll` is Kotlin and `removeIf` is Java but I don't understand why `removeAll` is there when `removeIf` was already there?

And for the fact that we could use `removeIf` in Kotlin without any hassle. Or is there any use case that might need one over another?
Reply

#2
Those are very very different functions, but in kotlin the method is overloaded, it means, it has two different types of arguments for the function.

If you read the doc, you can see that you can use this function by giving a collection:

fun <T> MutableCollection<out T>.removeAll(
elements: Collection<T>
): Boolean (source)

Or giving a function to filter:

fun <T> MutableIterable<T>.removeAll(
predicate: (T) -> Boolean
): Boolean (source)

So, the second one is like the Java version of removeIf, the first one is not.

Reply

#3
There is one more important difference:

Calling `removeIf` on a `CopyOnWriteArrayList` is **thread-safe**, but `removeAll` is **not**.

Looking at the code, `removeIf` has a custom implementation for `CopyOnWriteArrayList`, but `removeAll` **iterates over the indices** and will end up throwing `ArrayIndexOutOfBoundsException` or even worse, **removing the wrong element**, if called concurrently.
Reply

#4
Java's `removeIf()` is there since Java 1.8.

Kotlin started at 2011 ([wikipedia][1]). Java 1.8 appeared in 2014.

I'm not sure when the Kotlin's `removeAll(predicate)` was specified and implemented, however it probably predates Java's `removeIf()`.


[1]:

[To see links please register here]

Reply

#5
> Both of them yield the same result.

The result is achieved in completely different ways, so it might not always be quite same.

`removeAll` is a convenience method. It's a simple loop calling `removeAt` multiple times.

`removeIf` is a native `ArrayList` method that directly manipulates internal `elementData`, bypassing all the older removal methods and any logic that might have been put there. The bad news here is that it got retroactively blanket-applied to everything that derives from `ArrayList`, disregarding older implementations.

> Or is there any use case that might need one over another?

One example is `androidx.databinding.ObservableList`, which predates `removeIf`, and doesn't send due removal notifications, thus either breaking your code **silently** or causing [cryptic errors][1].



[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through