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:
  • 136 Vote(s) - 3.64 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I filter an ArrayList in Kotlin so I only have elements which match my condition?

#1
I have an array:

var month: List<String> = arrayListOf("January", "February", "March")

I have to filter the list so I am left with only `"January"`.
Reply

#2
You want to filter this list of Strings containing months.

<!-- language-all: kotlin -->

var month : List<String> = arrayListOf("January", "February", "March")

You can use [`filterNot()`](

[To see links please register here]

) method of list. It returns a list containing all elements except the given predicate.

var filteredMonthList : List<String> = month.filterNot { s -> s == "January" }
// results: ["February", "March"]

You can use [`filter()`](

[To see links please register here]

) method of list. It returns a list containing all elements matching the given predicate.

var filteredMonthList : List<String> = month.filter { s -> s == "January" }
// results: ["January"]

After `filter()` if we use [`single()`](

[To see links please register here]

) method then it will return a single value and throw an exception if more than one value is in the list.

var filteredMonth : String = month.filter { s -> s == "January" }.single()
// result: "January"
Reply

#3
There are a number of functions for filtering collections, if you want to keep only values matching `"January"`, you can use the simple [`filter()`](

[To see links please register here]

):

<!-- language-all: kotlin -->

val months = listOf("January", "February", "March")

months.filter { month -> month == "January" } // with explicit parameter name
months.filter { it == "January" } // with implicit parameter name "it"

These will give you a list containing only `"January"`.

If you want all months that are *not* `"January"`, you can either reverse the condition using `!=`, or use [`filterNot()`](

[To see links please register here]

):

months.filter { it != "January" }
months.filterNot { it == "January" }

These will give you a list containing `"February"` and `"March"`.

Note that unlike Java, using the `==` and `!=` operators in Kotlin is actually the same as calling the `equals` function on the objects. For more, see the docs about [equality](

[To see links please register here]

).

For the complete list of collection functions in the standard library, see the [API reference](

[To see links please register here]

).
Reply

#4
I am just sharing that if you have **custom list** and check whether it is **null or blank** you can check in Kotlin in single line
Just do it like that

fun filterList(listCutom: List<Custom>?) {
var fiterList = listCutom!!.filter { it.label != "" }
//Here you can get the list which is not having any kind of lable blank
}

You can check multiple conditions also

fun filterList(listCutom: List<Custom>?) {
var fiterList = listCutom!!.filter { it.label != "" && it.value != ""}
//Here you can get the list which is not having any kind of lable or value blank
}

Note : I am assuming that **label & value** are the variables of **Custom** Model class.
Reply

#5
You can also use [find][1] or `findLast`. This is specifically meant to return only one value instead of a list of `String` returned in case of `filter`.

var month = arrayListOf("January", "February", "March")
var result = month.find { s -> s == "January" }


[1]:

[To see links please register here]

Reply

#6
You can use this code to filter out January from array, by using this code

<!-- language-all: kotlin -->

var month: List<String> = arrayListOf("January", "February", "March")
// to get the result as list
var monthList: List<String> = month.filter { s -> s == "January" }

// to get a string
var selectedMonth: String = month.filter { s -> s == "January" }.single()
Reply

#7
**Filtering by predicate**

val numbers = listOf("one", "two", "three", "four")
var items: List<String> = numbers.filter { s -> s == "one" }

var item = numbers.singleOrNull { it == "one" }

if (item != null) {
print("FOUND:$item")
} else {
print("Not FOUND!")
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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