创建Stream流的四种方式

创建Stream方式1:通过集合List(常用)
创建Stream方式2:通过数组(常用)
创建Stream方式3:通过Stream的of()来创建
创建Stream方式4:创建无限流
详情见代码

public class StreamApiTest {


    /**
     * 创建Stream方式1:通过集合List
     */
    @Test
    public void test1() {

        List<Employee> emp = EmployeeData.getEmp();
        //通过集合来创建
        //顺序流
        Stream<Employee> stream = emp.stream();
        //并行流
        Stream<Employee> employeeStream = emp.parallelStream();
    }

    /**
     * 创建Stream方式2:通过数组
     */
    @Test
    public void test2() {
        int arr[] = new int[]{1, 2, 3, 4, 5, 6};
        //调用Arrays.stream返回数组
        IntStream stream = Arrays.stream(arr);

        Employee e1 = new Employee(1, "1", 1, 1);
        Employee e2 = new Employee(2, "1", 1, 1);

        Employee employees[] = new Employee[]{e1, e2};
        Stream<Employee> stream1 = Arrays.stream(employees);
    }


    /**
     * 创建Stream方式3: 通过Stream的of()来创建
     */
    @Test
    public void test3() {
        Stream<Integer> integerStream = Stream.of(1, 2, 3, 4, 5, 6);
        Stream<? extends Number> stream = Stream.of(1.0, 2, 3, 4, 5, 6);
    }

    /**
     * 创建Stream方式4:创建无限流
     */
    @Test
    public void test4() {
        //迭代
        //遍历前10个偶数
        Stream.iterate(0, t -> t + 2).limit(10).forEach(System.out::println);
        //生成
        Stream.generate(Math::random).limit(10).forEach(System.out::println);

    }

附上Employee.java和EmployeeData.java
Employee.java

public class Employee {

    private int id;
    private String name;
    private int age;
    private double salary;


    public Employee(int id) {
        this.id = id;
    }


    public Employee(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Employee)) return false;

        Employee employee = (Employee) o;

        if (getId() != employee.getId()) return false;
        if (getAge() != employee.getAge()) return false;
        if (Double.compare(employee.getSalary(), getSalary()) != 0) return false;
        return getName() != null ? getName().equals(employee.getName()) : employee.getName() == null;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = getId();
        result = 31 * result + (getName() != null ? getName().hashCode() : 0);
        result = 31 * result + getAge();
        temp = Double.doubleToLongBits(getSalary());
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public Employee() {
    }

    public Employee(int id, String name, int age, double salary) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

EmployeeData.java

public class EmployeeData {
    public static List<Employee> getEmp() {

        List<Employee> list = new ArrayList<>();
        list.add(new Employee(1001, "马化腾", 34, 6000.38));
        list.add(new Employee(1002, "马云", 12, 9876.12));
        list.add(new Employee(1003, "刘强东", 33, 3000.82));
        list.add(new Employee(1004, "雷军", 26, 7657.37));
        list.add(new Employee(1005, "李彦宏", 65, 5555.21));
        list.add(new Employee(1006, "比尔盖茨", 42, 9500.43));
        list.add(new Employee(1006, "任正非", 26, 4333.32));
        list.add(new Employee(1006, "扎克伯格", 35, 2500.32));
        return list;
    }
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值