具体代码如下所示: 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
具体代码如下所示:
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<Student> 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<Student> newList=list.stream().filter(item -> item.getAge().contains("G")).collect(Collectors.toList()); /*-----------filter的复杂应用-----------------------*/ // list.stream().filter(new Predicate<Student>(){ // // @Override // public boolean test(Student student) { // if(student.getName() !=null){ // return student.getName().contains("30"); // }else{ // return true; // } // } // }).collect(Collectors.toList()); /*-----------分页-----------------------*/ // List<Student> newList=list.stream().skip(4).limit(3).collect(Collectors.toList()); /*-----------map 返回一个新的list<Object>-----------------------*/ // List<Student> newList=list.stream().map(item ->{ // return new Student( // item.getAddress(), // item.getAge(), // item.getName(), // item.getId() // ); // }).collect(Collectors.toList()); /*-----------map 返回一个原来的的list<Student>并赋值-----------------------*/ // List<Student> newList=list.stream().map(item ->{ // item.setId("1"); // return item; // }).collect(Collectors.toList()); /*-----------flatMap-----------------------*/ // List<Student> newList= list.stream().flatMap(item ->{ // String[] split =item.getAddress().split(""); // String[] s2 = (String[]) Arrays.stream(split).filter(s -> s.contains("1")).toArray(); // return s2; // }); /*-----------sorted compareTo排序-----------------------*/ // List<Student> newList=list.stream().sorted((o1,o2)->{ // if(o1.getAddress().equals(o2.getAddress())){ // return Integer.parseInt(o1.getId())-Integer.parseInt(o2.getId()); // }else{ // return o1.getAddress().compareTo(o2.getAddress()); // } // }).collect(Collectors.toList()); /*-----------sorted Comparator排序-----------------------*/ // List<Student> newList=list.stream().sorted( // Comparator.comparing(Student::getAddress).reversed() // ).collect(Collectors.toList()); /*-----------sorted3 多条件排序-----------------------*/ // List<Student> newList=list.stream().sorted( // Comparator.comparing(Student::getAddress).thenComparing(Comparator.comparing(Student::getId).reversed()) // ).collect(Collectors.toList()); // boolean b=list.stream().anyMatch(item -> item.getAge().contains("G")); // System.out.println(b); /*-----------Collector tomap----------------------*/ // Map<String, String> newList=list.stream().sorted( // Comparator.comparing(Student::getAddress).thenComparing(Comparator.comparing(Student::getId).reversed()) // ).collect(Collectors.toMap(Student::getId,Student::getAddress)); /*-----------Collector groupingBy----------------------*/ Map<String, List<Student>> ageMap = list.stream().collect(Collectors.groupingBy(Student::getAddress)); System.out.println(ageMap ); } }
知识点扩展:
jdk 1.8 Lambda 表达式 遍历数组
Lambda 表达式可以把回调函数作为参数传递
常用的遍历数组的方式如下
List<String> datas = new ArrayList<>(); //遍历 datas.forEach(item->{ System.out.println(item); });
说明:
item就是每次遍历的当前元素
总结
以上所述是小编给大家介绍的Java JDK 1.8 lambda的用法详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对自由互联网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!