3.集合流学习之流的转换

package StreamStudy.Exa03;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 学习流的转换:
 * 涉及API: filter() 条件过滤 ,map() 结果转换 ,flatMap() 单子转换
 * limit() 裁剪 ,skip() 跳跃裁剪 ,concat()连接流 ,distinct() 去重
 * sorted() 排序 , peek() 方便查看流处理进度
 */
public class StreamChange {
    
    
    public static void main(String[] args) {
    
    
        String content="hello world, wo cao ni ma ma ni wo";
        String[] contents=content.split("\\PL+");
        Stream<String> words= Stream.of(contents);
        //******************************map与flatMap的使用********************************************
        //如果我我们想通过某种方式转换流中的元素,我们可以通过map()来实现
        Stream<String> changeWords=words.map(String::toUpperCase);
        List<String> result=changeWords.collect(Collectors.toList());
        System.out.println(result);
        //记住,流只能用一次,不可多次使用,用了就会关闭
        List<String> result2=Stream.of(contents).map(s->s.substring(0,1)).collect(Collectors.toList());
        System.out.println(result2);
        //下面有一个练习题:通过流获取一个字符串列表的所有平摊元素数组
        //使用一般的方式我们可以
        Stream<Stream<String>> result3=Stream.of(contents).map(w->letters(w));
        List<String> restr=new ArrayList<>();
        for(Stream<String> stream:result3.collect(Collectors.toList())){
    
    
            restr.addAll(stream.collect(Collectors.toList()));
        }
        System.out.println(restr);
        //上面的方式使用了流中流,这种方式显然比较复杂,下面有比较简便的方式
        List<String> restrs=Stream.of(contents).flatMap(w->letters(w)).collect(Collectors.toList());
        System.out.println(restrs);
        //******************************map与flatMap的使用********************************************

        //*************************limit,skip,concat,distinct的使用*********************************
        //limit用于裁剪流获得一个新的流,这个方法对于裁剪无限流特别方便
        Stream<Double> randoms=Stream.generate(Math::random).limit(10);//这个操作会产出一个包含10个随机数的流
        List<Double> randomList=randoms.collect(Collectors.toList());
        System.out.println(randomList);

        //skip用于跳过当前流n个元素获得之后的元素集的流
        List<Double> skipList=Stream.generate(Math::random).limit(10).skip(5).collect(Collectors.toList());
        System.out.println(skipList);

        //concat用于将两个流连接起来
        List<Double> concatList=Stream.concat(Stream.generate(Math::random).limit(5),Stream.generate(Math::random).limit(5)).collect(Collectors.toList());
        System.out.println(concatList);

        //distinct用于去除重复元素
        List<String> distinctList=Stream.of(contents).distinct().collect(Collectors.toList());
        System.out.println(distinctList);
        //*************************limit,skip,concat,distinct的使用*********************************

        //*************************sorted,peek的使用*****************************************
        //sorted的使用
        List<String> sortedList=Stream.of(contents).sorted().collect(Collectors.toList());//如果sorted中不给比较器,按默认排序
        System.out.println(sortedList);
        List<String> sortedList2=Stream.of(contents).sorted(Comparator.comparing(String::length)).collect(Collectors.toList());//按元素长度升序排序
        System.out.println(sortedList2);
        List<String> sortedList3=Stream.of(contents).sorted(Comparator.comparing(String::length).reversed()).collect(Collectors.toList());//按元素长度降序排序
        System.out.println(sortedList3);

        //peek的使用,使用peek,没取一个元素时都会执行一次peek中的函数
        List<String> peekList=Stream.of(contents).peek(e-> System.out.println("元素"+e)).limit(5).collect(Collectors.toList());
        System.out.println(peekList);

    }

    /**
     * 此方法的作用是将字符串中的各个字符拆分开来存入流中
     * @param s 字符串
     * @return 包含字符串字符的流
     */
    public static Stream<String> letters(String s){
    
    
        List<String> result=new ArrayList<>();
        for(int i=0;i<s.length();i++){
    
    
            result.add(s.substring(i,i+1));
        }
        return result.stream();
    }
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/c1776167012/article/details/121839695