千家信息网

Java中JDK1.8lambda怎么用

发表于:2024-12-05 作者:千家信息网编辑
千家信息网最后更新 2024年12月05日,这篇文章给大家分享的是有关Java中JDK1.8lambda怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。具体代码如下所示:public class Student
千家信息网最后更新 2024年12月05日Java中JDK1.8lambda怎么用

这篇文章给大家分享的是有关Java中JDK1.8lambda怎么用的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

具体代码如下所示:

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-----------------------*///    List newList=list.stream().map(item ->{//      return new Student(//          item.getAddress(),//          item.getAge(),//          item.getName(),//          item.getId()//      );//    }).collect(Collectors.toList());    /*-----------map 返回一个原来的的list并赋值-----------------------*///    List newList=list.stream().map(item ->{//      item.setId("1");//      return item;//    }).collect(Collectors.toList());    /*-----------flatMap-----------------------*///    List 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 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 newList=list.stream().sorted(//        Comparator.comparing(Student::getAddress).reversed()//    ).collect(Collectors.toList());    /*-----------sorted3 多条件排序-----------------------*///    List 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 newList=list.stream().sorted(//        Comparator.comparing(Student::getAddress).thenComparing(Comparator.comparing(Student::getId).reversed())//    ).collect(Collectors.toMap(Student::getId,Student::getAddress));     /*-----------Collector groupingBy----------------------*/    Map> ageMap = list.stream().collect(Collectors.groupingBy(Student::getAddress));    System.out.println(ageMap );  }}

感谢各位的阅读!关于"Java中JDK1.8lambda怎么用"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

0