lamda 表达式
// 新建 Person类 用于测试的数据源
public class Person {
private String firstName;
private String lastName;
private Stringjob;
private String gender;
private int salary;
private intage ;
public Person(String firstName, String lastName, String job,
String gender, int age, int salary) {
this.firstName = firstName;
this.lastName = lastName;
this.gender = gender;
this.age = age;
this.job = job;
this.salary = salary;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
// 测试类
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class Streams {
// 1. 不需要参数,返回值为 5
// () -> 5
//
// 2. 接收一个参数(数字类型),返回其2倍的值
// x -> 2 * x
//
// 3. 接受2个参数(数字),并返回他们的差值
// (x, y) -> x – y
//
// 4. 接收2个int型整数,返回他们的和
// (int x, int y) -> x + y
//
// 5. 接受一个 string 对象,并在控制台打印,不返回任何值(看起来像是返回void)
// (String s) -> System.out.print(s)
public void calc(){ //新建集合 做一个简单的排序输出
List<Integer> list =new ArrayList<>();
list.add(1);
list.add(23);
list.add(88);
list.add(26);
list.add(64);
Collections.sort(list, (x, y) -> y - x);
list.forEach((aa)->System.out.print(aa+" ")); // 第一种输出方式
list.forEach(System.out::println); // 第二种输出方式
}
// 第一个测试
//public static void main(String[] args) { // 运行测试一下calc() 方法
//new Streams().calc();
//}
// 第二个测试
//public static void main(String[] args) {
//String[] players = {"Rafael Nadal", "Novak Djokovic",
//"Stanislas Wawrinka", "David Ferrer",
//"Roger Federer", "Andy Murray",
// "Tomas Berdych", "Juan Martin Del Potro",
//"Richard Gasquet", "John Isner"}; // 定义一个数组 进行排序 然后输出
//
//Arrays.sort(players, (String s1, String s2) -> (s1.compareTo(s2))); // 对数组 进行 排序
//List<String> lists = Arrays.asList(players); // 把排序后的数组 转为集合
//lists.forEach((play)->System.out.println(play)); // 输出集合内容
//}
// 第三个测试
//public static void main(String[] args) {
//List<Person> programmers = new ArrayList<Person>() { // 定义一个 List 集合
//add(new Person("Jarrod", "Pace", "PHP programmer", "male", 34, 1550));
//add(new Person("Clarette","Cicely" , "PHP programmer", "female",23 , 1200));
//add(new Person("Victor", "Channing","PHP programmer" ,"male" , 32, 1600));
//add(new Person("Tori", "Sheryl", "PHP programmer" ,"female",21 ,1000 ));
//add(new Person("Osborne", "Shad", "PHP programmer", "male", 32, 1100));
//add(new Person("Rosalind","Layla" , "PHP programmer", "female",25 , 1300));
//add(new Person("Fraser", "Hewie", "PHP programmer", "male", 36, 1100));
//add(new Person("Quinn", "Tamara", "PHP programmer", "female",21 , 1000));
//add(new Person("Alvin", "Lance", "PHP programmer", "male", 38, 1600));
//add(new Person("Evonne", "Shari", "PHP programmer", "female",40 , 1800));
//add(new Person("Jack", "Tgasd", "PHP programmer", "female",42 , 1600));
//add(new Person("Rose", "Ebacr", "PHP programmer", "female",36 , 1600));
//add(new Person("Smith", "Ftaaw", "PHP programmer", "female",21 , 1600));
//add(new Person("Dove", "Drace", "PHP programmer", "female",19 , 1600));
//add(new Person("Lamda", "Amabg", "PHP programmer", "female",53 , 1600));
//add(new Person("Grails", "Bhacy", "PHP programmer", "female",42 , 1600));
//};
// System.out.println("下面是月薪超过 1400人员 :");
// programmers.stream()
// .filter((p) -> (p.getSalary() > 1400)) // 过滤 薪资大于1400 的所有人员
// .forEach((p) -> System.out.printf("%s-%s; ", p.getFirstName(), p.getLastName()));
// System.out.println();
// System.out.println("下面是月薪等于 1000 的人员:");
// programmers.stream()
//.filter((p) -> ("Osborne".equals(p.getFirstName()))) //// 过滤 姓 是Osborne
//.forEach((p) -> System.out.println(p.getFirstName()+p.getLastName()));
//System.out.println();
//System.out.println("工资等于 $1600的人员 按年龄排序:默认 从大到小");
//List<Person> pg = programmers.stream().filter(pgs -> 1600==pgs.getSalary())
//.sorted((aud1,aud2) -> aud2.getAge().compareTo(aud1.getAge())) // 1、比较函数 进行排序
// 2、比较函数前加负号可降序排列 .sorted((aud1,aud2) -> -(aud2.getAge().compareTo(aud1.getAge())))
//.collect(Collectors.toList());
//pg.forEach((p) -> System.out.println(p.getFirstName()+"-"+p.getLastName()+" "+p.getSalary()+" "+p.getAge()));
//}
}