Java8新特性之Steam

2022/8/6 1:23:52

本文主要是介绍Java8新特性之Steam,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

package com.demo;

import org.testng.annotations.Test;

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

public class StreamTest {

    @Test
    public void test1() {
        //实例化Steam
        List<People> list = People.getPeopleList();
        Stream<People> stream = list.stream();
        // 1. 筛选与切片
        System.out.println("只打印第一个=============");
        stream.limit(1).forEach(System.out::println);
        // 流被终止后只能重新实例化
        System.out.println("跳过前两个============");
        list.stream().skip(2).forEach(System.out::println);
        System.out.println("筛选出性别为女的============");
        list.stream().filter(e -> e.getSex().equals("女")).forEach(System.out::println);

        // 2. 映射,可以理解为会对集合里每个元素都进行的一个操作
        System.out.println("打印出每个对象的名字============");
        list.stream().map(People::getName).forEach(System.out::println);
        System.out.println("在每个对象名字后面加上一个+号并打印出来============");
        list.stream().map(People::getName).map(e -> e += "+").forEach(System.out::println);

        //3. 排序
//        System.out.println("排序会报错,因为People没有实现comparable接口============");
//        list.stream().sorted().forEach(System.out::println);
        System.out.println("根据年龄排序============");
        list.stream().sorted(
                (o1, o2) -> {

                    if (o1.getAge() == o2.getAge()) {
                        return o1.getName().compareTo(o2.getName());
                    }
                    return Integer.compare(o1.getAge(), o2.getAge());
                }
        ).forEach(System.out::println);

        //4. 匹配与查找
        System.out.println("是否所有的对象都是男性============");
        System.out.println(list.stream().allMatch(e -> e.getSex().equals("男")));
        System.out.println("是否有一个对象是男性============");
        System.out.println(list.stream().anyMatch(e -> e.getSex().equals("男")));
        System.out.println("是否没有一个对象是男性============");
        System.out.println(list.stream().noneMatch(e -> e.getSex().equals("男")));
        System.out.println("打印出第一个对象============");
        System.out.println(list.stream().findFirst());

        //5.规约
        System.out.println("求所有对象年龄的综合============");
        Optional<Integer> reduce = list.stream().map(People::getAge).reduce(Integer::sum);
        System.out.println(reduce);
        System.out.println("把所有性别为男的对象的年龄放到一个新的列表里面============");
        List<Integer> collect = list.stream().filter(e -> e.getSex().equals("男")).map(People::getAge).collect(Collectors.toList());
        System.out.println(collect);

    }


}

class People {
    String name;
    String sex;
    int age;

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

    public static List<People> getPeopleList() {
        List<People> peopleList = new ArrayList<>();
        People people = new People("雷军", "男", 45);
        People people1 = new People("马云", "男", 45);
        People people2 = new People("任正非", "男", 77);
        People people3 = new People("董明珠", "女", 44);
        peopleList.add(people);
        peopleList.add(people1);
        peopleList.add(people2);
        peopleList.add(people3);
        return peopleList;
    }

    @Override
    public String toString() {
        return "People{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", 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;
    }
}




这篇关于Java8新特性之Steam的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程