Java8之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<Student> 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<Student> l1 = list1.stream().filter(student -> student.getSex().equals("男")).collect(toList());
        System.out.println("求性别为男生的学生集合:");
        System.out.println(l1);
 
        //map的key值true为男,false为女的集合
        Map<Boolean,List<Student>>  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<String, Integer> 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<String> 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<Student> l3 = list1.stream().sorted((s1, s2) -> s1.getAge().compareTo(s2.getAge())).collect(toList());
        System.out.println("按照年龄从小到大排序:");
        System.out.println(l3);
 
        //求年龄最小的两个学生
        List<Student> 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 +
                    '}';
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值