Created
October 4, 2019 15:48
-
-
Save kwmt/5febb6271f8e5c817e05d20404150eca to your computer and use it in GitHub Desktop.
delete duplicate objects of list / 重複したリストのオブジェクトを削除したい
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
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