Stream流式计算
package com.zkw.JUC并发编程.functiom.stream;
public class User {
private int ID;
private String name;
private int age;
public User() {
}
public User(int ID, String name, int age) {
this.ID = ID;
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "User{" +
"ID=" + ID +
", name='" + name + '\'' +
", age=" + age +
'}';
}
package com.zkw.JUC并发编程.functiom.stream;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) {
User u1 = new User(1,"a",21);
User u2 = new User(2,"b",22);
User u3 = new User(3,"c",23);
User u4 = new User(4,"d",24);
User u5 = new User(6,"e",25);
List<User> list = Arrays.asList(u1, u2, u3, u4, u5);
list.stream()
.filter(u ->{return u.getID()%2==0;})
.filter(u ->{return u.getAge()>23;})
.map(u ->{return u.getName().toUpperCase();})
.sorted((name1,name2)->{return name2.compareTo(name1);})
.limit(1)
.forEach(System.out::println);
}
}