Created
March 14, 2020 14:40
-
-
Save magdamiu/e96b835637323ab061bbd67006acb732 to your computer and use it in GitHub Desktop.
Search functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//first | |
//NoSuchElementException could be thrown | |
println(languagesList.first()) | |
println(languagesList.first { it.name.length == 3 }) | |
println(languagesList.firstOrNull { it.name.length == 2 }) | |
//find | |
println(languagesList.find { it.town.equals("San Francisco") }) | |
println(languagesList.findLast { it.town.equals("Bucharest") }) | |
//last | |
println(languagesList.last()) | |
println(languagesList.last { it.town.contains(' ') }) | |
//single | |
//IllegalArgumentException if list has more than one element | |
println(languagesList.single{ it.town.equals("San Francisco")}) | |
println(languagesList.singleOrNull{ it.town.equals("Pitesti")}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment