JAVA 使用 Stream 中 Collectors 工具类 基础操作
求取最大值、最小值、求和、求平均值
class A { companion object { private val list = ArrayList<Test>() @RequiresApi(Build.VERSION_CODES.N) @JvmStatic fun main(args: Array<String>) { println("-----------------------------------") list.add(Test(120, "语文")) list.add(Test(12, "数学")) list.add(Test(70, "英语")) list.add(Test(92, "化学")) list.add(Test(10, "物理")) list.add(Test(96, "生物")) //求取最大值 val maxBy = list.stream().collect(Collectors.maxBy(Comparator.comparing(Test::score))) //求取最小值 val minBy = list.stream().collect(Collectors.minBy(Comparator.comparing(Test::score))) //平均值 averagingInt\averagingDouble、averagingInt、averagingLong val averagingInt = list.stream().collect(Collectors.averagingInt(Test::score)) println("-maxBy----${maxBy}-minBy-${minBy}---averagingInt-${averagingInt}") //summarizingDouble、summarizingInt、summarizingLong TODO 求得:计数、求平均、求和、最大、最小 val summarizingInt = list.stream().collect(Collectors.summarizingInt(Test::score)) println("-summarizingInt--总数--${summarizingInt.count} 总分--${summarizingInt.sum} 平均分--${summarizingInt.average} 最小--${summarizingInt.min} 最大--${summarizingInt.max} ") } } data class Test(var score: Int, val suject: String = "") }