Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter a list in-place with Kotlin?

In Java I can remove items from a list with this code:

private void filterList(List<Item> items) {     Iterator<Item> iterator = items.iterator();     while (iterator.hasNext()) {         if (checkItem(iterator.next())) {             iterator.remove();         }     } } 

How to make the same in Kotlin (i.e. remove some items in a List without re-creation)?

like image 596
BArtWell Avatar asked Jun 21 '17 11:06

BArtWell


People also ask

How do you filter string Kotlin?

To filter characters in a String in Kotlin, use String. filter() method. Given a string str1 , and if we would like to filter the characters of this string using a predicate (some condition) predicate , call filter() method on string str1 and pass the predicate predicate as argument to the method as shown below.


2 Answers

Just use .retainAll { ... } or .removeAll { ... }, both accepting a predicate, to filter it in-place:

items.retainAll { shouldRetain(it) } 

items.removeAll { shouldRemove(it) } 

Note that items should be a MutableList<T> for that, not just List<T>, which is a read-only list in Kotlin and thus does not expose any mutating functions (see: Collections in the language reference).

By the way, these two function are implemented efficiently for lists that support random access: then the list is not compacted after each item is removed (O(n2) time worst-case), and instead the items are moved within the list as it is processed, giving O(n) time.


And if you don't want to modify the original list, you can produce a separate collection with only the items you want to retain using .filter { ... } or .filterNot { ... }, this will work for read-only List<T> as well:

val filtered = items.filter { shouldRetain(it) } 

val filtered = items.filterNot { shouldRemove(it) } 
like image 83
hotkey Avatar answered Sep 24 '22 13:09

hotkey


Kotlin has a lot of neat built-in functions. You can try to use filter here.

val filteredItems = items.filter { checkItem(it) }   

Unfortunately, it will recreate the list. This API was designed on purpose to avoid extra mutability.

But if you still want to proceed with a MutableList use retainAll method

items.retainAll { checkItem(it) } 
like image 40
Konstantin Kiriushyn Avatar answered Sep 25 '22 13:09

Konstantin Kiriushyn