gekson
gekson

Reputation: 99

How to use MutableList to remove with kotlin call java?

I have a MutableList with generic with Int, like MutableList. I wonder how to use kotlin call java method remove(int position) and remove(Integer object) correctly?

public void remove(int position) {
    if (this.list != null && this.list.size() > position) {
        this.list.remove(position);
        notifyDataSetChanged();
    }
}

public void remove(T t) {
    if (t != null && this.list != null && !this.list.isEmpty()) {
        boolean removed = false;
        try {
            removed = this.list.remove(t);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (removed) {
            notifyDataSetChanged();
        }
    }
}

Upvotes: 1

Views: 6752

Answers (3)

gekson
gekson

Reputation: 99

Finally i find the answer in Kotlin doc by myself. Kotlin will box generics int to Integer if you declare the variable as Int?, because only an Object can be null.You can use the tool in android studio which can show the kotlin bytecode to find out that.

If we call java method like the question image:

val a:Int = 0
remove(a) // will call java method remove(int position)

val a:Int? = 0
remove(a) // will call java method remove(T t) to remove object

the result is different! If have better choice, we should avoid use like that.

Upvotes: 1

gil.fernandes
gil.fernandes

Reputation: 14601

There are overall 4 methods for removing items in kotlin.collections.MutableList as of Kotlin 1.2.30:

  • remove - used to remove element once. Calls to this method are compiled to calls to the Java method List.remove(Object o).
  • removeAt - used to remove at a certain position. Calls to this method are compiled to calls to the Java method List.remove(int index).
  • removeAll - used to remove a collection, each element multiple times
  • removeIf - remove using a predicate, each element multiple times

Below is an example of how you can use each method. In the comments you can find what each method would print to the console and a brief explanation of what it does:

fun main(args: Array<String>) {
    val l: MutableList<Int> = mutableListOf<Int>(1, 2, 3, 3, 4, 5, 6, 7, 1, 1, 1)
    println(l.remove(1))               // true
    println(l)                         // [2, 3, 3, 4, 5, 6, 7, 1, 1, 1] - removes first element and stops
    println(l.removeAt(0))             // 2 - removes exactly on a specific position
    println(l)                         // [3, 3, 4, 5, 6, 7, 1, 1, 1]
    try {
        println(l.removeAt(10000))
    } catch(e: IndexOutOfBoundsException) {
        println(e)                            // java.lang.IndexOutOfBoundsException: Index: 10000, Size: 9
    }
    println(l.removeAll(listOf(3, 4, 5)))     // true
    println(l)                                // [6, 7, 1, 1, 1] - removes elements in list multiple times, 3 removed multiple times
    println(l.removeIf { it == 1 })           // true
    println(l)                                // [6, 7] - all ones removed
}

...

true
[2, 3, 3, 4, 5, 6, 7, 1, 1, 1]
2
[3, 3, 4, 5, 6, 7, 1, 1, 1]
java.lang.IndexOutOfBoundsException: Index: 10000, Size: 9
true
[6, 7, 1, 1, 1]
true
[6, 7]

Upvotes: 3

ice1000
ice1000

Reputation: 6569

Seems that other answers just give alternatives of remove(int position) but they didn't answer the question directly (according to the edit).

So you can use this code:

val list = mutableListOf("ah!", "I", "died!")
(list as java.util.List<String>).remove(1)
println(list)

Result:

[ah!, died!]

That's it! You've successfully invoked remove(int position) from Java.
This will raise some warnings, just ignore them.

Try it online!

Upvotes: 0

Related Questions