Stream流综合练习

package LambdaTest.LambdaTest06;

import java.util.ArrayList;
import java.util.stream.Stream;

/**
 * FileName: StreamsynthesizeDemo
 * Author:   lps
 * Date:     2022/4/10 13:30
 * Sign:刘品水 Q:1944900433
 */
public class StreamSynthesizeDemo {
    public static void main(String[] args) {
        ArrayList<String> array = new ArrayList<>();
        array.add("刘品水");
        array.add("张伟");
        array.add("刘品火");
        array.add("大张伟");
        array.add("刘一手");
        array.add("刘红");

        ArrayList<String> womenArray = new ArrayList<>();
        womenArray.add("张阳");
        womenArray.add("迪丽热巴");
        womenArray.add("杨幂");
        womenArray.add("张敏");
        womenArray.add("赵丽颖");
        womenArray.add("金晨");
        //男要名字三个字的前三个人
        //array.stream().filter(s->s.length()==3).limit(3).forEach(System.out::println);
        Stream<String> s1 = array.stream().filter(s -> s.length() == 3).limit(3);
        //System.out.println("=========");
        //女要张姓且不要第一个
        //womenArray.stream().skip(1).filter(s->s.startsWith("张")).forEach(System.out::println);
        Stream<String> s2 = womenArray.stream().skip(1).filter(s -> s.startsWith("张"));
        //System.out.println("=========");
        //把过滤和后的男演员和女演员合并
        //static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b)
        //创建一个懒洋洋的级联流的元素的所有元素的第一流通过第二个流的元素。
        //Stream.concat(s1,s2).distinct().forEach(System.out::println);
        Stream<String> stream = Stream.concat(s1, s2).distinct();

        //stream.map(Actor::new).forEach(System.out::println);
        stream.map(Actor::new).forEach(s-> System.out.println(s.getName()));

        System.out.println("============");
        Stream.concat(array.stream().filter(s -> s.length()==3).limit(3),
                womenArray.stream().skip(1).filter(s -> s.startsWith("张"))).
                map(Actor::new).forEach(s-> System.out.println(s.getName()));

    }
}

 

 

posted @ 2022-04-10 13:59  刘品水  阅读(139)  评论(0编辑  收藏  举报