Skip to content

Instantly share code, notes, and snippets.

@PriyaSindkar
Last active August 24, 2022 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PriyaSindkar/d09c62c76539d42d95f192f89d1cc70e to your computer and use it in GitHub Desktop.
Save PriyaSindkar/d09c62c76539d42d95f192f89d1cc70e to your computer and use it in GitHub Desktop.
fun main() {
val listOfMumbaiPeople = listOf<Person>(
Person("John", Address("Thane", "Mumbai")),
Person("Bob", Address("Bandra", "Mumbai"))
)
val listOfVadodaraPeople = listOf<Person>(
Person("Jane", Address("Manjalpur", "Vadodara")),
Person("Alice", Address("Akota", "Vadodara"))
)
val allPeople = listOf(listOfMumbaiPeople, listOfVadodaraPeople)
val people = allPeople.flatten()
// filter people from mumbai
val peopleFromMumbai = people.filter { it.address.city == "Mumbai" }
print("\npeopleFromMumbai = $peopleFromMumbai")
// check if any person from the list id from Mumbai
val isAnyoneFromMumbai = people.any { it.address.city == "Mumbai" }
print("\nisAnyoneFromMumbai = $isAnyoneFromMumbai")
// check if all people from the list are from Mumbai
val areAllFromMumbai = people.all { it.address.city == "Mumbai" }
print("\nareAllFromMumbai = $areAllFromMumbai")
// check if none of the people from the list are from Surat
val isNooneFromSurat = people.all { it.address.city == "Surat" }
print("\nisNooneFromSurat = $isNooneFromSurat")
// check if there are any residents from Thane, Mumbai
val isAnyoneFromThaneMumbai = people.filter { it.address.city == "Mumbai" }.any { it.address.addressLine1 == "Thane" }
print("\nisAnyoneFromThaneMumbai = $isAnyoneFromThaneMumbai")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment