java8 新特性 lambda过滤
生活随笔
收集整理的這篇文章主要介紹了
java8 新特性 lambda过滤
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1. 定義實體類
package com.atguigu.java8;public class Employee {private int id;private String name;private int age;private double salary;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 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 "測試方法引用!";}@Overridepublic 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;}@Overridepublic 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;}@Overridepublic String toString() {return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + "]";}}2. 測試類
package com.atguigu.java8;import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.TreeSet;import org.junit.Test;public class TestLambda1 {List<Employee> emps = Arrays.asList(new Employee(101, "張三", 18, 9999.99),new Employee(102, "李四", 59, 6666.66),new Employee(103, "王五", 28, 3333.33),new Employee(104, "趙六", 8, 7777.77),new Employee(105, "田七", 38, 5555.55));//需求:獲取公司中年齡小于 35 的員工信息public List<Employee> filterEmployeeAge(List<Employee> emps){List<Employee> list = new ArrayList<>();for (Employee emp : emps) {if(emp.getAge() <= 35){list.add(emp);}}return list;}//需求:獲取公司中工資大于 5000 的員工信息public List<Employee> filterEmployeeSalary(List<Employee> emps){List<Employee> list = new ArrayList<>();for (Employee emp : emps) {if(emp.getSalary() >= 5000){list.add(emp);}}return list;}//優(yōu)化方式四:Stream API @Testpublic void test7(){emps.stream().filter((e) -> e.getAge() <= 35).forEach(System.out::println);System.out.println("----------------------------------------------");emps.stream().map(Employee::getName).limit(3).sorted().forEach(System.out::println);} }?
轉(zhuǎn)載于:https://www.cnblogs.com/mengjianzhou/p/8997799.html
總結(jié)
以上是生活随笔為你收集整理的java8 新特性 lambda过滤的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python字符串27种常见的方法
- 下一篇: 面试题:四种Java线程池用法解析 !