《JAVA》Stream 常见用法

package streams;


import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * Stream 常用练习
 */
public class StreamTest {
    @Test
    public void studyStream() {
        /*
          filter()方法
          API释义:返回由与此给定谓词匹配的此流的元素组成的流。
          个人理解:根据条件(lambda表达式) 筛选符合条件的数据, 返回是流,通常和collect()方法结合使用
          练习:筛选数字大于15的元素并返回一个List。
        */
        List<Integer> nums = Arrays.asList(10, 18);
        System.out.println(nums
                .stream()
                .filter(num -> num > 15)
                .collect(Collectors.toList()));
        /*
          map()方法
          API释义:返回由给定函数应用于此流的元素的结果组成的流。
          个人理解:根据给定的方法获取流中的元素,返回的是流,通常和collect()方法结合使用。
          练习:有一个学生类的集合,获取所有学生的学号,并返回一个List集合。
          ps:学生类有名字和学号两个属性。
        */
        Student student1 = Student.builder().num(1).name("张三").build();
        Student student2 = Student.builder().num(2).name("李四").build();
        List<Student> students = Arrays.asList(student1, student2);
        List<Integer> numList = students
                .stream()
                .map(student -> student.num)
                .collect(Collectors.toList());
        System.out.println(numList);
        /*
          flatMap()方法
          API释义:操作具有对流的元素应用一对多变换,然后将所得到的元素平坦化为新流的效果。
          个人理解:将多个集合中所有的元素进行平铺后合并成一个新流。
          练习:有一个学校类的集合,每一个学校里边有个学生类集合,获取所有学校下所有学生的姓名
        */
        Student student3 = Student.builder().num(1).name("王五").build();
        Student student4 = Student.builder().num(2).name("赵六").build();
        List<Student> student2s = Arrays.asList(student3, student4);
        School school1 = new School();
        school1.setStudentList(students);
        School school2 = new School();
        school2.setStudentList(student2s);
        List<School> schoolList = Arrays.asList(school1, school2);
        // 获取所有学生的集合
        List<Student> allStudentList = schoolList.stream()
                .flatMap(school -> school.getStudentList().stream())
                .collect(Collectors.toList());
        // 获取所有学生的名字
        List<String> allNameList = allStudentList.stream()
                .map(student -> student.name)
                .collect(Collectors.toList());
        System.out.println("flatMap: 所有学生的名字为:" + allNameList);
        /*
          mapToInt() ( mapToLong()、mapToDouble() )方法
          API释义:返回一个IntStream (LongStream、DoubleStream),其中包含将给定函数应用于此流的元素的结果。
          个人理解:根据给定的方法将流中的元素转换成int型并返回,返回的是流,通常和collect()方法结合使用。
          练习:有一个字符串的集合,返回集合内字符串的长度总和。
        */
        List<String> strList = Arrays.asList("abc", "de");
        int sums = strList.stream().mapToInt(String::length).sum();
        /*
          flatMapToInt() ( flatMapToLong()、flatMapToDouble() )方法
          API释义:返回一个IntStream (LongStream、DoubleStream),其中包含将给定函数应用于此流的元素的结果。
          个人理解:根据给定的方法将流中的元素转换成int型并返回,返回的是流,通常和collect()方法结合使用。
          练习:有一个字符串的集合的集合,返回集合内字符串的长度总和。
        */
        int allStrSums = strList.stream().flatMapToInt(strings -> IntStream.of(strings.length())).sum();
        System.out.println("flatMapToInt: 所有字符串长度:" + allStrSums);

        /*
          distinct()
          API释义:返回由该流的不同元素(根据Object.equals(Object) )组成的流。
          个人理解:去重
         */
        List<String> strList2 = Arrays.asList("abc", "de", "abc");
        List<String> res = strList2.stream().distinct().collect(Collectors.toList());
        System.out.println("distinct() 去重结果: " + res);

        /*
          sorted()
          API释义:返回由此流的元素组成的流,根据自然顺序排序。(根据提供的Comparator进行排序)
          个人理解:排序
         */
        List<Integer> nums1 = Arrays.asList(1,5,3,6);
        // 自然排序
        System.out.println(nums1.stream().sorted().collect(Collectors.toList()));
        // 倒序
        System.out.println(nums1.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()));

        /*
           peek()
         */

        List<Product> ps = new ArrayList<>();
        Product pro = Product.builder().category("书籍").price(101).build();
        Product pro1 = Product.builder().category("书籍").price(99).build();
        Product pro2 = Product.builder().category("球类").price(101).build();
        ps.add(pro);
        ps.add(pro1);
        ps.add(pro2);

        // 练习一  获取属于书籍类别,且价格》100的产品列表
        List<String> result = ps.stream()
                .filter(product -> product.category.equals("书籍") && product.price > 100)
                .map(product -> product.category)
                .collect(Collectors.toList());
    }
}

@Builder
class Student {
    public int num;
    public String name;
}

@Getter
@Setter
class School {
    public int num;
    public List<Student> studentList;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值