具体代码如下所示:
public class Student { private String id; private String name; private String age; private String address; public Student(String id, String name, String age, String address) { this.id = id; this.name = name; this.age = age; this.address = address; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Student{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", age='" + age + '\'' + ", address='" + address + '\'' + '}'; }}public class TestStrean { public static void main(String[] args){ Student stuA = new Student("1", "A", "M", "184"); Student stuB = new Student("2", "B", "G", "163"); Student stuC = new Student("3", "C", "M", "175"); Student stuD = new Student("4", "D", "G", "158"); Student stuE = new Student("5", "E", "M", "175"); List list = new ArrayList<>(); list.add(stuA); list.add(stuB); list.add(stuC); list.add(stuD); list.add(stuE); /*-----------forEach-----------------------*/// list.forEach(item -> item.setId(item.getAddress()+"test") ); /*-----------filter-----------------------*/// List newList=list.stream().filter(item -> item.getAge().contains("G")).collect(Collectors.toList()); /*-----------filter的复杂应用-----------------------*/// list.stream().filter(new Predicate(){//// @Override// public boolean test(Student student) {// if(student.getName() !=null){// return student.getName().contains("30");// }else{// return true;// }// }// }).collect(Collectors.toList()); /*-----------分页-----------------------*/// List newList=list.stream().skip(4).limit(3).collect(Collectors.toList()); /*-----------map 返回一个新的list