stream流

重要性

  • jdk8最重要的两大特性:lambdastream
  • stream流:用于操作数组([])或者集合(Collection)的数据
  • 优势:stream流大量的结合了lambda的风格,代码更加简洁可读性更好

使用步骤

在这里插入图片描述

代码示例List

  • 一半在处理完数据之后,我们会重新把数据转换为Collection,所以就需要用到collect方法
  • 此方法参数:一个Collector,用来转换为新集合
List<String> nameList = new ArrayList<>();
Collections.addAll(nameList, "张三丰", "张无忌", "张翠山", "张三", "赵敏", "周芷若", "张强");
// 1.过滤出姓张的 2.长度大于2的 3.转换为List
List<String> newNameList = nameList.stream().filter(name -> name.startsWith("张")).filter(name -> name.length() > 2)
        .collect(Collectors.toList());
System.out.println(newNameList); 

代码示例-Map

 Map<String,Integer> nameMap = new HashMap<>();
    nameMap.put("张三丰", 100);
    nameMap.put("张无忌", 200);
    nameMap.put("张翠山", 300);
    nameMap.put("张三", 400);
    nameMap.put("赵敏", 500);
    nameMap.put("周芷若", 600);

    // 1.过滤出key以张开头的 2.值大于200的 3.转换为Map
    Map<String,Integer> newNameMap = nameMap.entrySet().stream(). // 获取stream流
            filter(entry -> entry.getKey().startsWith("张")). // 过滤出key以张开头的
            filter(entry -> entry.getValue() > 200) // 过滤出值大于200的
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); // 终结操作,转换为Map
    System.out.println(newNameMap);

获取stream流

在这里插入图片描述

中间方法

  • 中间方法:所谓的中间方法处理完数据之后,会返回新的stream流,继续处理数据,这种编程方式又称为链式编程

在这里插入图片描述

数据

List<Student> studentList = new ArrayList<>();
studentList.add(new Student("张三丰", 100, 100, 1.80));
studentList.add(new Student("张三丰", 120, 80, 1.80));
studentList.add(new Student("张无忌", 20, 100, 1.70));
studentList.add(new Student("张翠山", 50, 40, 1.60));
studentList.add(new Student("张三", 30, 60, 1.70));
studentList.add(new Student("赵敏", 40, 80, 1.60));
studentList.add(new Student("周芷若", 60, 90, 1.70));

过滤和排序

// 1.过滤出年龄大于30的 2.分数大于60的 3.升序排序 4.转换为List
// filter() 过滤
// sorted() 排序 参数是Comparator接口
List<Student> newStudentList = studentList.stream()
        .filter(student -> student.getAge() > 30)
        .filter(student -> student.getScore() > 60)
        .sorted((s1, s2) -> s1.getScore() - s2.getScore())
        .collect(Collectors.toList());
System.out.println(newStudentList);

限制

// 2.找出成绩最高的3个学生
// limit(3) 限制最多3个元素
List top3StudentList = studentList.stream()
.sorted((s1, s2) -> s2.getScore() - s1.getScore())
.limit(3)
.collect(Collectors.toList());
System.out.println(top3StudentList);

去重

// 3.获取所有学生的名字 不可以重复 转为List<String>
// map() 转换 参数是Function接口
// distinct() 去重
List<String> nameList = studentList.stream()
        .map(Student::getName)
        .distinct()
        .collect(Collectors.toList());
System.out.println(nameList);

自定义去重

// 4.去重之后找到年龄最大的4个学生(名字相同的视为同一个学生)
// 自定义去重
List<Student> max4StudentList = studentList.stream()
        .filter(customDistinct())
        .sorted((s1, s2) -> s2.getAge() - s1.getAge())
        .limit(4)
        .collect(Collectors.toList());
System.out.println(max4StudentList);

private static Predicate<Student> customDistinct() {
    final Map<String,Student > map = new ConcurrentHashMap<>();
    return student -> map.put(student.getName(), student) == null;
}

跳过

// 5.跳过
List skipStudentList = studentList.stream()
.skip(2)
.collect(Collectors.toList());
System.out.println(skipStudentList);

常见的终结方法-处理

在这里插入图片描述

// 1. 统计分数大于60的学生个数
long count = studentList.stream()
      .filter(student -> student.getScore() > 60)
      .count();
System.out.println(count);

// 2. 统计分数大于60的学生平均分
double average = studentList.stream()
      .filter(student -> student.getScore() > 60)
      .mapToInt(Student::getScore)
      .average()
      .getAsDouble();
System.out.println(average);

// 3. 统计分数大于60的学生总分
int sum = studentList.stream()
      .filter(student -> student.getScore() > 60)
      .mapToInt(Student::getScore)
      .sum();
System.out.println(sum);

// 4. 统计分数大于60的学生最高分
int max = studentList.stream()
      .filter(student -> student.getScore() > 60)
      .mapToInt(Student::getScore)
      .max()
      .getAsInt();
System.out.println(max);

// 5. 统计分数大于60的学生最低分
int min = studentList.stream()
      .filter(student -> student.getScore() > 60)
      .mapToInt(Student::getScore)
      .min()
      .getAsInt();
System.out.println(min);

常见终结方法-收集stream流

  • 把stream流中的结果再次返回到数组或者集合
  • stream流只能收集一次,被收集过的流会被关闭,无法再收集

在这里插入图片描述

List<String> newNameList = nameList.stream().filter(name -> name.startsWith("张")).filter(name -> name.length() > 2)
        .collect(Collectors.toList());
System.out.println(newNameList);

转换为Map的时候,有两个必要参数,谁是键,谁是值

// 1.过滤出key以张开头的 2.值大于200的 3.转换为Map
Map<String,Integer> newNameMap = nameMap.entrySet().stream(). // 获取stream流
        filter(entry -> entry.getValue() > 200) // 过滤出值大于200的
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); // 终结操作,转换为Map

toArray:转换为数组

//将所有名字转换为大写 转换为Array
String[] nameArray = nameList.stream().map(String::toUpperCase).toArray(String[]::new);
  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值