Skip to content

Instantly share code, notes, and snippets.

@kwmt
Created October 4, 2019 15:48
Show Gist options
  • Save kwmt/5febb6271f8e5c817e05d20404150eca to your computer and use it in GitHub Desktop.
Save kwmt/5febb6271f8e5c817e05d20404150eca to your computer and use it in GitHub Desktop.
delete duplicate objects of list / 重複したリストのオブジェクトを削除したい
data class User(val name : String, val latLng: LatLng)
data class LatLng(val lat: Int, val lng: Int)
fun deleteDuplicates() {
// create data
val results = (1..10).map {User("$it", LatLng(it % 4, it%4))}
results.forEach {
println(it)
}
println()
// grouping by latlng
val r2 = results.groupingBy { it.latLng }
// delete duplicate objects
val r3 = r2.reduce { key, accumulator, element ->
if(accumulator.latLng == element.latLng) {
accumulator
} else element
}
println()
r3.forEach {
println(it)
}
println()
r3.values.forEach {
println(it)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment