How can I filter an ArrayList in Kotlin so I only have elements which match my condition?

kotlin filter list of objects
kotlin filter list by another list
kotlin list filter multiple conditions
kotlin filter nested list
kotlin find object in list
kotlin filter mutable list
kotlin map
kotlin list to arraylist

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".

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

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()

Filtering Collections, Returns a list containing only elements matching the given predicate. A straightforward explanation of Kotlin’s List.filter. Other Methods for Filtering Lists. In addition to List.filter, there are a number of other filtering methods that have specialized purposes

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

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():

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.

For the complete list of collection functions in the standard library, see the API reference.

filter, A quick and practical reference for filtering Kotlin collections. To read more about lambdas, have a look at our Kotlin Lambda article here. the list until we reach an element that does not meet the condition: the items that match the predicate into the returned List, we use the filter to perform that action. 11 Kotlin : null cannot be value of non null type kotlin Jan 27 '18 8 Sliding BottomSheet like google map Dec 16 '15 8 How can I filter an ArrayList in Kotlin so I only have elements which match my condition?

You want to filter this list of Strings containing months.

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

You can use filterNot() 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() 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() 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"

Filtering Kotlin Collections, A straightforward explanation of Kotlin's List.filter. of an ArrayList but remember, it's 2004 and we're going retro here). that match the condition in the lambda, it filters things that do not match. The filterIsInstance method filters a List to contain only those elements that are instances of a particular class. 3. ArrayList indexOf() example to check if element exists. As noted above, contains() method uses indexOf() method to determine if a specified element is present in the list or not. So we can also directly use the indexOf() method to check the existance of any supplied element value.

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.

Kotlin List API — Part 1: List Filtering | by dashfwd, The filtering conditions are defined by predicates – lambda functions that take a given element matches the predicate, and false means it doesn't match the predicate. Kotlin program of using the filter function on list and Map collection – if collection has elements and false if its empty; none() do just opposite of any(). Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

You can also use find 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" }

Kotlin, Kotlin distinguishes between read-only and mutable lists. Read-only lists are We have methods to get the first and the last elements of the list. KotlinListFirstLast. The filter() method takes a predicate function as a parameter . The predicate gives a condition that the elements must meet. We filter out words � In this tutorial, we will show you few Java 8 examples to demonstrate the use of Streams filter(), collect(), findAny() and orElse(). 1. Streams filter() and collect() 1.1 Before Java 8, filter a List like this :

Kotlin lists tutorial - working with lists in Kotlin, Note: By default, read and write access to your database is restricted so only By using these auto-generated keys for each new element in the list, several clients Even when there is only a single match for the query, the snapshot is still a list; You can also filter the sorted result to a specific number of results or a range� In the above program, we have an array of integers stored in variable num. Likewise, the number to be found is stored in toFind. Now, we use a foreach loop to iterate through all elements of num and check individually if toFind is equal to n or not. If yes, we set found to true and break from the loop. If not, we move to the next iteration.

Work with Lists of Data on Android, The stream noneMatch() method works just opposite to the anyMatch() method, false if any of the stream elements matches the condition specified by the predicate. ArrayList; class Student{ int stuId; int stuAge; String stuName; Student(int id, should start with letter "P" and age must be <28 * Stream element matched:� Output : Modified ArrayList : [10, 20, 30] This article is contributed by Nitsdheerendra.If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to

Java 8 Stream – noneMatch() example, In this program, you'll learn to sort an arraylist of custom object by their given In the main() method, we've created an array list of custom objects list takes a comparator compareBy that compares customProperty of each object and sorts it. Do not sell my data; Privacy Policy � Terms & Conditions � App's Privacy Policy � Star the project and contribute to Kotlin. Twitter → Keep up with the latest Kotlin news. Slack → Join the discussion about Kotlin. Reddit → Chat with the community and the team. Stack Overflow → Find the best solutions for you

  • Please clarify the question that you want "January" filtered OUT of the list (removed) or filter the list to ONLY have "January" ... The question is being interpreted in the answers in a way that seems opposite of what makes sense.
  • It is a good idea for new Kotlin developers to scan the STDLIB API reference guide so you have an idea of what is available for use. And if coming from Java 8 streams, you can get an idea of equivalents in…
  • I needed to filter out the month January. I have edited the question
  • Note that in Kotlin you do not have to use .equals explicitly. It would be more idiomatic to just use == which will do the right thing.
  • this is the opposite of the request, they want to remove "January" from the list not keep only January, please update the answer to be correct, otherwise the accepted answer should be moved to answer from zsmb13 which covers both cases.
  • Kotlin is sexy!
  • You can simplify it even further with: month.single { s -> s == "January" }
  • How would this change if you had a list of custom Objects, and not just some built-in type like Int, or String?
  • How would I filter out multiple values? Say, I would like both "January" and "March" filtered out of the list?
  • You could do something like months.filter { it !in listOf("January", "March" }.
  • This is the opposite of what was asked.
  • The question asks to filter the month, which means to remove it. You do the opposite and filter everything BUT the month. The question should be worded a bit more clearly as "filter out the month"
  • @JaysonMinard Please check the answer now.
  • @JaysonMinard Thanks. The answer is looking awesome.