Java JDK1.8Stream流常见用法

转自参考:Java8之list.stream的常见使用_岁月安然的博客-CSDN博客_list stream

JDK1.8新特性stream流常见的一些list、map计算、分类的使用方法

package com.test;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.*;

public class Test8 {

    public static void main(String[] args) {
        List list1 = new ArrayList<>();
        list1.add(new Student("赵一", "男", 18));
        list1.add(new Student("钱二", "男", 20));
        list1.add(new Student("孙三", "女", 19));
        list1.add(new Student("李四", "女", 22));
        list1.add(new Student("周五", "男", 24));
        list1.add(new Student("吴六", "女", 21));
        list1.add(new Student("郑七", "女", 25));
        list1.add(new Student("王八", "女", 21));
        //求性别为男生的学生集合
        List l1 = list1.stream().filter(student -> student.getSex().equals("男")).collect(toList());
        System.out.println("求性别为男生的学生集合:");
        System.out.println(l1);

        //map的key值true为男,false为女的集合
        Map>  map = list1.stream().collect(partitioningBy(student -> student.getSex().equals("男")));
        System.out.println("map的key值true为男,false为女的集合:");
        System.out.println(map.get(true));
        System.out.println(map.get(false));

        //求性别为男的学生总岁数
        Integer sum = list1.stream().filter(student -> student.sex.equals("男")).mapToInt(Student::getAge).sum();
        System.out.println("求性别为男的学生总岁数:");
        System.out.println(sum);


        //按性别进行分组统计人数
        Map map1 = list1.stream().collect(Collectors.groupingBy(Student::getSex, Collectors.summingInt(p -> 1)));
        System.out.println("按性别进行分组统计人数:");
        System.out.println(map1);


        //判断是否有年龄大于25岁的学生
        boolean check = list1.stream().anyMatch(student -> student.getAge() > 25);
        System.out.println("判断是否有年龄大于25岁的学生:");
        System.out.println(check);


        //获取所有学生的姓名集合
        List l2 = list1.stream().map(Student::getName).collect(toList());
        System.out.println("获取所有学生的姓名集合:");
        System.out.println(l2);

        //求所有人的平均年龄
        double avg = list1.stream().collect(averagingInt(Student::getAge));
        System.out.println("求所有人的平均年龄:");
        System.out.println(avg);

        //求年龄最大的学生,方法一
        Student s = list1.stream().reduce((student, student2) -> student.getAge() > student2.getAge() ? student:student2).get();
        System.out.println("求年龄最大的学生,方法一:");
        System.out.println(s.toString());

        //求年龄最大的学生,方法一
        Student stu = list1.stream().collect(maxBy(Comparator.comparing(Student::getAge))).get();
        System.out.println("求年龄最大的学生,方法二:");
        System.out.println(stu.toString());

        //按照年龄从小到大排序
        List l3 = list1.stream().sorted((s1, s2) -> s1.getAge().compareTo(s2.getAge())).collect(toList());
        System.out.println("按照年龄从小到大排序:");
        System.out.println(l3);

        //求年龄最小的两个学生
        List l4 = l3.stream().limit(2).collect(toList());
        System.out.println("求年龄最小的两个学生:");
        System.out.println(l4);


        //获取所有的名字,组成一条语句
        String str = list1.stream().map(Student::getName).collect(Collectors.joining(",", "[", "]"));
        System.out.println("获取所有的名字,组成一条语句:");
        System.out.println(str);

        //获取年龄的最大值、最小值、平均值、求数量等等
        IntSummaryStatistics intSummaryStatistics = list1.stream().mapToInt(Student::getAge).summaryStatistics();
        System.out.println("获取年龄的最大值、最小值、平均值、求数量等等:");
        System.out.println(intSummaryStatistics.getMax());
        System.out.println(intSummaryStatistics.getMin());
        System.out.println(intSummaryStatistics.getAverage());
        System.out.println(intSummaryStatistics.getCount());

    }

    static class Student{
        String name;
        String sex;
        Integer age;

        public Student(String name, String sex, Integer age) {
            this.name = name;
            this.sex = sex;
            this.age = age;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", sex='" + sex + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
}

你可能感兴趣的:(java,后端)