java8_02_stream(一)创建流和中间操作

Stream 主要分为三部分 1. 创建流 2.中间操作 3.终止操作

一、创建流的四种方法
  1. Collection系列集合的stream() parallelStream() 方法

    Stream stringStream = list.stream();

  2. Arrays.stream(T[] t)
    Stream studentStream = Arrays.stream(students);
  3. Stream.of(T… values)
    Stream stringStream = Stream.of(“aa”,”bb”,”cc”);
  4. 无限流

    • 迭代

      Stream.iterate(0, (x) -> x+2 )
      .forEach(System.out::println);

    • 生成

      Stream.generate(()-> Math.random())
      .forEach(System.out::println);

二、中间操作

多个中间操作可以连接起来形成一个流水线,除非流水线上触发终止操作,否则中间操作不会执行任何处理。而在终止操作是一次性全部处理,称为“惰性求值

2.1 筛选与切片
 List<Student> students = Arrays.asList(
            new Student("张三",18,88),
            new Student("李四",18,80),
            new Student("王五",19,60),
            new Student("王五",19,60),
            new Student("王五",19,60),
            new Student("赵六",17,100)
    );
    // 1.过滤
    @Test
    public void test(){
        //中间操作
        Stream<Student> studentStream = students.stream()
                .filter(s -> s.getAge() > 17);
        //终止操作
        studentStream.forEach(System.out::println);
}
//2.limit(n) :前几个 
    @Test
    public void test2(){
        students.stream()
                .filter(s-> s.getScore()>70)
                .limit(2)
                .forEach(System.out::println);
    }
//3. skip(n) 跳过前n个
    @Test
    public void test3(){
        students.stream()
                .filter(s->s.getAge()>17)
                .skip(1) //跳过
                .forEach(System.out::println);
    }
  // 4. distinct() 去重
    @Test
    public void test4(){
        students.stream()
                .filter(s->s.getAge()>17)
                .skip(1) //跳过
                .distinct() //去重,需要对象重写hashCode() equals() 两个方法
                .forEach(System.out::println);
    }
2.2 映射 map和flatMap
  • map : 接收 Lambda , 将元素转换成其他形式或提取信息。接收一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素。
 public static Stream<Character> getCharacterStream(String str){
        List<Character> characters = new ArrayList<>();
        for (Character c: str.toCharArray()) {
            characters.add(c);
        }
        return characters.stream();
    }
  @Test
  public void testMap(){
        List<String> list = Arrays.asList("aaa","bbb","ccc");
        Stream<Stream<Character>> chStream = list.stream()
                .map(s-> getCharacterStream(s));
        chStream.forEach(stream -> {
            stream.forEach(System.out::println);
        });
        System.out.println("--------------------------------");
    }
  • flatMap : 接收一个函数作为参数,将流中的每个值都换成另一个流,然后把所有流连接成一个流
    @Test
    public void testFlatMap(){
        List<String> list = Arrays.asList("aaa","bbb","ccc");
        Stream<Character> flatStream = list.stream()
                .flatMap(s-> getCharacterStream(s));
        flatStream.forEach(System.out::println);
    }

总结:
- map返回一个流,或者多个流的集合
- flatMap 是每个元素都合并到一个流中,最终产生一个流

2.3 排序
  • sorted() : 自然排序
  • sorted(Comparator com) :定制排序
/*
默认排序
*/
 @Test
    public void testSorted(){
        List<String> list = Arrays.asList("aaa","BBB","AAA","aaa","ccc","CCC");
                 list.stream()
                     .sorted()
                     .forEach(System.out::println);
    }

//数据
 List<Student> students = Arrays.asList(
            new Student("张三",18,88),
            new Student("李四",18,80),
            new Student("王五",19,60),
            new Student("王五",19,60),
            new Student("王五",19,60),
            new Student("赵六",17,100)
    );
 //自定义排序
 @Test
    public void testSorted2(){
        //先转换(List<T>->List<U>),然后排序
        students.stream()
                .map(Student::getAge)
                .sorted(Comparator.comparingInt(s -> s))
                .forEach(System.out::println);
        //直接排序
        students.stream()
                .sorted((s1,s2)->{
                    if (s1.getAge() == s2.getAge()){
                        return Integer.compare(s1.getScore(),s2.getScore());
                    }else
                        return Integer.compare(s1.getAge(),s2.getAge());
                })
                .forEach(System.out::println);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值