Preface: This part is the difference between java and unique, it is equivalent to the method you good package, you only need a simple line of code, you can complete java need a lot of lines of code work.
Usage of high-level functions
Define a class and a set Girl
data class Girl(var name: String, var age: Int, var height: Int, var address: String)
var girlDB = listOf<Girl>(
Girl("aa", 18, 180, "河南"),
Girl("bb", 28, 180, "广西"),
Girl("cc", 38, 180, "浙江"),
Girl("dd", 48, 180, "北京"),
Girl("ee", 28, 190, "河南"),
Girl("ff", 28, 180, "广东")
)
The oldest data output
fun main(args: Array<String>) {
println(girlDB.maxBy { it.age })
}
filter to filter the output
println(girlDB.filter {
(it.age<30) and (it.height>180) and (it.age>18)
})
map output
var result = girlDB.map {
"${it.name} : ${it.age}"
}
println(result)
count output
println(girlDB.count{
it.age>18
})
groupBy output
var result = girlDB.groupBy {
it.address
}.get("河南")?.forEach{println(it)}
println(result)
Usage of custom dsl
In the method of adding the class Girl
infix fun List<Girl>.query(age:Int){
filter {
it.age <age
}.forEach (::println)
}
Then call Main
fun main(args: Array<String>) {
girlDB query 50
}
Ok! End.