Java8新特性_Stream_查找与匹配

 

package com.zs.boot.controller;

public class Employee implements Comparable<Employee>{

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

	public Employee() {
	}

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

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

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

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

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

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

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

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}

	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 String show() {
		return "测试方法引用!";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + age;
		result = prime * result + id;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		long temp;
		temp = Double.doubleToLongBits(salary);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Employee other = (Employee) obj;
		if (age != other.age)
			return false;
		if (id != other.id)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))
			return false;
		return true;
	}

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

	@Override
	public int compareTo(Employee o) {
		if(String.valueOf(this.getAge()).equals(o.getAge())){
			return this.getName().compareTo(o.getName());
		}else{
			return String.valueOf(this.getAge()).compareTo(String.valueOf(o.getAge()));
		}
	}

	public enum Status {
		FREE, BUSY, VOCATION;
	}



}
package com.zs.boot.controller;

import org.junit.Test;

import java.util.*;
import java.util.stream.Stream;

public class TestStream3API {

    List<Employee> employees = Arrays.asList(
            new Employee("刘德华",48,8000, Employee.Status.FREE),
            new Employee("张学友",28,8500, Employee.Status.BUSY),
            new Employee("黎明",45,6500, Employee.Status.VOCATION),
            new Employee("郭富城",51,5500, Employee.Status.FREE));

    @Test
    public void test1(){
        boolean b1 = employees.stream().allMatch(e -> e.getStatus().equals(Employee.Status.FREE));
        System.out.println(b1);//false,不是匹配所有的元素

        boolean b2 = employees.stream().anyMatch(e -> e.getStatus().equals(Employee.Status.FREE));
        System.out.println(b2);//true,匹配一个就可以

        boolean b3 = employees.stream().noneMatch(e -> e.getStatus().equals(Employee.Status.VOCATION));
        System.out.println(b3);//false,是不是没有匹配的

        //按工资排序,找出第一个
        Optional<Employee> employee = employees.stream().sorted((e1,e2) ->
            Double.compare(e1.getSalary(),e2.getSalary())).findFirst();
        //-Double.compare(e1.getSalary(),e2.getSalary()),加-查询工资最高的
        System.out.println(employee.get());//Employee [id=0, name=郭富城, age=51, salary=5500.0, status=FREE]

        //找出处于空闲状态的任意一位
        Optional<Employee> op = employees.stream().filter(e -> e.getStatus().equals(Employee.Status.FREE)).findAny();
        System.out.println(op.get());

        //上面是串行流,可以使用并行流parallelStream()
        Optional<Employee> op2 = employees.parallelStream().filter(e -> e.getStatus().equals(Employee.Status.FREE)).findAny();
        System.out.println(op2.get());
    }


    @Test
    public void test2() {
        //返回流中元素的总个数
        Long count = employees.stream().count();
        System.out.println(count);//4

        //返回流中最大值
        Optional<Employee> op = employees.stream().max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(op.get());//Employee [id=0, name=张学友, age=28, salary=8500.0, status=BUSY]

        //返回流中最小值,这次要求找出最小的工资,先用map提取出来,然后用min过滤
        Optional<Double> op2 = employees.stream().map(Employee::getSalary).min(Double::compare);
        System.out.println(op2.get());//5500.0

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZHOU_VIP

您的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值