Home > Software engineering >  Intersection of two lists in kotlin
Intersection of two lists in kotlin

Time:10-11

How can we intersect two lists in kotlin and save it in another variable(collection of String)

for example I have two lists like this

val list: MutableList<JSONArray> = Arrays.asList(requestedFields)
val otherList: MutableList<MutableList<String>> = Arrays.asList(requiredFields)

Any help is highly appreciated, Thanks

CodePudding user response:

You can use the intersect method: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/intersect.html

For example:

val first = listOf("Ragh", "Cat")
val second = listOf("Dog", "Ragh", "Giraffe")
    
val third = first.intersect(second)
    
println(third) // prints [Ragh]
  • Related