jdk新特性(二)stream流

stream流

为什么需要stream流,
1、Stream流的引入可以使编程更加优雅,去除冗余代码,增加代码运行效率。
2、Stream流把真正的函数式编程风格引入到Java中。

案例

public class Actor {
    private String name;
    private String sex;
    private int age;

    public Actor() {
    }

    public Actor(String name, String sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }


    public void setFeatures(String name, String sex, int age){
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    @Override
    public String toString() {
        return "StreamTest{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}

测试类

public class StreamTest {
    public static void main(String[] args) {
        ArrayList<Actor> actors = new ArrayList<>();
        Actor[] act = new Actor[12];
        for (int i = 0; i < act.length; i++) {
            act[i]=new Actor();
        }
        act[0].setFeatures("赵丽颖","女",34);
        act[1].setFeatures("杨颖","女",39);
        act[2].setFeatures("李易峰","男",36);
        act[3].setFeatures("郭德纲","男",61);
        act[4].setFeatures("杨幂","女",38);
        act[5].setFeatures("李沁","女",35);
        act[6].setFeatures("张子健","男",47);
        act[7].setFeatures("何家劲","男",52);
        act[8].setFeatures("金超群","男",56);
        act[9].setFeatures("萨顶顶","女",44);
        act[10].setFeatures("杨紫","女",28);
        act[11].setFeatures("陈乔恩","女",39);

        System.out.println("--------------处理前--------------");
        for (Actor actor : act) {
            actors.add(actor);
            System.out.println(actor);
        }

        System.out.println("---------------处理后----------");
        Stream<Actor> womanStream = actors.stream()
                .filter((s) -> "女".equals(s.getSex()))
                .filter(s -> s.getName().startsWith("杨"))
                .filter(s -> s.getAge() > 35);

        Stream<Actor> manStream = actors.stream().filter((s) -> s.getName().length() > 2)
                .filter(s->"男".equals(s.getSex()))
                .filter(s->s.getAge()>35)
                .skip(1)
                .limit(3);

        Stream<Actor> concat = Stream.concat(manStream, womanStream);
        System.out.println("-----------------------------");
        //用集合来收集
        Map<String, Integer> collect = concat.collect(Collectors.toMap(s -> s.getName(), s -> s.getAge()));
        Set<Map.Entry<String, Integer>> entries = collect.entrySet();
        for (Map.Entry<String, Integer> entry :
                entries) {
            System.out.println(entry.getKey()+":"+entry.getValue());
        }
        /*System.out.println("-----------------------");
        Set<String> strings = collect.keySet();
        for (String string : strings) {
            System.out.println(string+":"+collect.get(string));
        }*/
        //List<Actor> collect = concat.collect(Collectors.toList());
        //System.out.println(collect);
        // concat.map(Actor::getName).forEach(System.out::println);
    }
}

测试结果
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/xueshanfeitian/article/details/108975142