Java8之lambda表达式基本语法-创新互联

lambda表达式,即带有参数的表达式,为更清晰地理解lambda表达式,先看如下例子:

网站建设哪家好,找成都创新互联!专注于网页设计、网站建设、微信开发、重庆小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了浦口免费建站欢迎大家使用!

(1)

class Student{
  private String name;
  private Double score;
  public Student(String name, Double score) {
    this.name = name;
    this.score = score;
  }
  public String getName() {
    return name;
  }
  public Double getScore() {
    return score;
  }
  public void setName(String name) {
    this.name = name;
  }
  public void setScore(Double score) {
    this.score = score;
  }
  @Override
  public String toString() {
    return "{"
        + "\"name\":\"" + name + "\""
        + ", \"score\":\"" + score + "\""
        + "}";
  }
}
@Test
public void test1(){
  List studentList = new ArrayList(){
    {
      add(new Student("stu1",100.0));
      add(new Student("stu2",97.0));
      add(new Student("stu3",96.0));
      add(new Student("stu4",95.0));
    }
  };
  Collections.sort(studentList, new Comparator() {
    @Override
    public int compare(Student o1, Student o2) {
      return Double.compare(o1.getScore(),o2.getScore());
    }
  });
  System.out.println(studentList);
}

分享名称:Java8之lambda表达式基本语法-创新互联
网站网址:http://scyanting.com/article/dehhge.html