标签:方法 支持 信息 false 函数式 3.2 stl 语言 需要
package com.xuweiwei; import org.junit.Test; import java.util.Comparator; import java.util.Set; import java.util.TreeSet; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public class TestLambda { @Test public void test1(){ Set<Integer> set = new TreeSet<>(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return Integer.compare(o1,02); } }); set.add(500); set.add(100); System.out.println(set); } }
package com.xuweiwei; import org.junit.Test; import java.util.Comparator; import java.util.Set; import java.util.TreeSet; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public class TestLambda { @Test public void test2(){ Comparator<Integer> comparator = (o1,o2) ->Integer.compare(o1,o2); Set<Integer> set = new TreeSet<>(comparator); set.add(-1); set.add(500); set.add(50); System.out.println(set); } }
package com.xuweiwei; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public class Employee { private String name; private Integer age; private Double salary; public Employee() { } public Employee(String name, Integer age, Double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } @Override public String toString() { return "Employee{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ", salary=" + salary + ‘}‘; } }
package com.xuweiwei; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public class TestLambda { List<Employee> employees = Arrays.asList( new Employee("张三",18,1111.11), new Employee("李四",28,2222.22), new Employee("王五",38,4444.44), new Employee("赵六",55,9999.99) ); //需求:获取当前公司中员工年龄大于30的员工信息 public List<Employee> filterEmployess(List<Employee> employees){ List<Employee> employeeList = new ArrayList<>(); for (Employee employee : employees) { Integer age = employee.getAge(); if(null != age && age >= 35){ employeeList.add(employee); } } return employeeList; } @Test public void test3(){ List<Employee> employeeList = filterEmployess(employees); System.out.println(employeeList); } }
package com.xuweiwei; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public class Employee { private String name; private Integer age; private Double salary; public Employee() { } public Employee(String name, Integer age, Double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } @Override public String toString() { return "Employee{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ", salary=" + salary + ‘}‘; } }
package com.xuweiwei; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public class TestLambda { List<Employee> employees = Arrays.asList( new Employee("张三",18,1111.11), new Employee("李四",28,2222.22), new Employee("王五",38,4444.44), new Employee("赵六",55,9999.99) ); //需求:获取当前公司中员工年龄大于30的员工信息 public List<Employee> filterEmployess(List<Employee> employees){ List<Employee> employeeList = new ArrayList<>(); for (Employee employee : employees) { Integer age = employee.getAge(); if(null != age && age >= 35){ employeeList.add(employee); } } return employeeList; } //获取公司员工工资大于5000的员工信息 public List<Employee> filterEmployees2(List<Employee> employees){ List<Employee> employeeList = new ArrayList<>(); for (Employee employee : employees) { Double salary = employee.getSalary(); if(null != salary && salary >= 5000){ employeeList.add(employee); } } return employeeList; } @Test public void test3(){ List<Employee> employeeList = filterEmployees2(employees); System.out.println(employeeList); } }
package com.xuweiwei; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public class Employee { private String name; private Integer age; private Double salary; public Employee() { } public Employee(String name, Integer age, Double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } @Override public String toString() { return "Employee{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ", salary=" + salary + ‘}‘; } }
package com.xuweiwei; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public interface MyPredicate<T> { boolean test(T t); }
package com.xuweiwei; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public class FilterEmployeeByAge implements MyPredicate<Employee> { @Override public boolean test(Employee employee) { if(null != employee){ Integer age = employee.getAge(); if(null != age && age >= 35){ return true; } } return false; } }
package com.xuweiwei; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public class TestLambda { List<Employee> employees = Arrays.asList( new Employee("张三",18,1111.11), new Employee("李四",28,2222.22), new Employee("王五",38,4444.44), new Employee("赵六",55,9999.99) ); public List<Employee> filterEmployess(List<Employee> employees,MyPredicate<Employee> mp){ List<Employee> employeeList = new ArrayList<>(); for (Employee employee : employees) { if(mp.test(employee)){ employeeList.add(employee); } } return employeeList; } @Test public void test3(){ List<Employee> employeeList = filterEmployess(employees,new FilterEmployeeByAge()); System.out.println(employeeList); } }
package com.xuweiwei; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public class Employee { private String name; private Integer age; private Double salary; public Employee() { } public Employee(String name, Integer age, Double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } @Override public String toString() { return "Employee{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ", salary=" + salary + ‘}‘; } }
package com.xuweiwei; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public interface MyPredicate<T> { boolean test(T t); }
package com.xuweiwei; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public class TestLambda { List<Employee> employees = Arrays.asList( new Employee("张三",18,1111.11), new Employee("李四",28,2222.22), new Employee("王五",38,4444.44), new Employee("赵六",55,9999.99) ); public List<Employee> filterEmployess(List<Employee> employees,MyPredicate<Employee> mp){ List<Employee> employeeList = new ArrayList<>(); for (Employee employee : employees) { if(mp.test(employee)){ employeeList.add(employee); } } return employeeList; } @Test public void test3(){ List<Employee> employeeList = filterEmployess(employees, new MyPredicate<Employee>() { @Override public boolean test(Employee o) { if(o.getAge() >35){ return true; } return false; } }); System.out.println(employeeList); } }
package com.xuweiwei; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public class Employee { private String name; private Integer age; private Double salary; public Employee() { } public Employee(String name, Integer age, Double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } @Override public String toString() { return "Employee{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ", salary=" + salary + ‘}‘; } }
package com.xuweiwei; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public interface MyPredicate<T> { boolean test(T t); }
package com.xuweiwei; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public class TestLambda { List<Employee> employees = Arrays.asList( new Employee("张三",18,1111.11), new Employee("李四",28,2222.22), new Employee("王五",38,4444.44), new Employee("赵六",55,9999.99) ); public List<Employee> filterEmployess(List<Employee> employees,MyPredicate<Employee> mp){ List<Employee> employeeList = new ArrayList<>(); for (Employee employee : employees) { if(mp.test(employee)){ employeeList.add(employee); } } return employeeList; } @Test public void test3(){ List<Employee> employeeList = filterEmployess(employees, (e) -> e.getAge() >= 35 ); employeeList.forEach(System.out::println); } }
package com.xuweiwei; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public class Employee { private String name; private Integer age; private Double salary; public Employee() { } public Employee(String name, Integer age, Double salary) { this.name = name; this.age = age; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } @Override public String toString() { return "Employee{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ", salary=" + salary + ‘}‘; } }
package com.xuweiwei; import org.junit.Test; import java.util.Arrays; import java.util.List; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生最爱笑 */ public class TestLambda { List<Employee> employees = Arrays.asList( new Employee("张三", 18, 1111.11), new Employee("李四", 28, 2222.22), new Employee("王五", 38, 4444.44), new Employee("赵六", 55, 9999.99) ); @Test public void test3() { employees.stream().filter((e) -> e.getSalary() >= 2000).limit(2).forEach(System.out::println); } }
() -> System.out.println("hello");
package com.xuweiwei; import org.junit.Test; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生只爱笑 */ public class TestLambda2 { @Test public void test1(){ //匿名内部类的形式 Runnable runnable = new Runnable() { @Override public void run() { System.out.println("hello world!!"); } }; //Lambda表达式的方式 runnable = () -> System.out.println("hello lambda"); } }
package com.xuweiwei; import org.junit.Test; import java.util.function.Consumer; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生只爱笑 */ public class TestLambda2 { @Test public void test1(){ Consumer<String> consumer = (e) -> System.out.println(e); consumer.accept("你好啊"); } }
package com.xuweiwei; import org.junit.Test; import java.util.function.Consumer; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生只爱笑 */ public class TestLambda2 { @Test public void test1(){ Consumer<String> consumer = e -> System.out.println(e); consumer.accept("你好啊"); } }
package com.xuweiwei; import org.junit.Test; import java.util.Comparator; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生只爱笑 */ public class TestLambda2 { @Test public void test1(){ Comparator<Integer> comparator = (o1,o2) -> { System.out.println("函数式接口"); return Integer.compare(o1,o2); }; } }
package com.xuweiwei; import org.junit.Test; import java.util.Comparator; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生只爱笑 */ public class TestLambda2 { @Test public void test1(){ Comparator<Integer> comparator = (x,y) -> Integer.compare(x,y); } }
package com.xuweiwei; import org.junit.Test; import java.util.Comparator; /** * @description: * @verion: * @since: * @motto: 不为往事扰,余生只爱笑 */ public class TestLambda2 { @Test public void test1(){ Comparator<Integer> comparator = (Integer x,Integer y) -> Integer.compare(x,y); comparator = (x,y) -> Integer.compare(x,y); } }
2 函数式接口
3 方法引用和构造器引用
4 Stream API
5 接口中的默认方法和静态方法
6 新时间日期API
7 其它的新特性
标签:方法 支持 信息 false 函数式 3.2 stl 语言 需要
原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/9704185.html