当前位置: 移动技术网 > IT编程>开发语言>Java > Java8之lambda表达式基本语法

Java8之lambda表达式基本语法

2019年07月19日  | 移动技术网IT编程  | 我要评论

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<student> studentlist = new arraylist<student>(){
    {
      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<student>() {
    @override
    public int compare(student o1, student o2) {
      return double.compare(o1.getscore(),o2.getscore());
    }
  });
  system.out.println(studentlist);
}

(1)中代码调用collections.sort方法对集合进行排序,其中第二个参数是一个类,准确地说是一个匿名内部类,sort方法调用内部类中的compare方法对list进行位置交换,因为java中的参数类型只能是类或者基本数据类型,所以虽然传入的是一个comparator类,但是实际上需要传递的仅仅是compare方法,lambda表达式专门针对只有一个方法的接口(即函数式接口),comparator就是一个函数式接口 

@functionalinterface
public interface comparator<t> {
  int compare(t o1, t o2);
}

@functionalinterface的作用就是标识一个接口为函数式接口,此时comparator里只能有一个抽象方法。

使用lambda表达式之后(1)中的代码改造如下

(2)

public void test1_(){
    list<student> studentlist = new arraylist<student>(){
      {
        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,(s1,s2)-> double.compare(s1.getscore(),s2.getscore()));
    system.out.println(studentlist);
  }

对于有多个参数的情况,语法:

     1. ambda表达式的基本格式为(x1,x2)->{表达式...};

     2. 在上式中,lambda表达式带有两个参数,因此两边的括号不能省略,而参数类型可以省略

     3. 如果表达式只有一行,那么表达式两边的花括号可以省略

另外一个常见的例子是新建一个线程,不使用lambda表达式的写法为

(3)

public void testthread(){
    new thread(new runnable() {
      @override
      public void run() {
        system.out.println("hello, i am thread!");
      }
    }).start();
  }

其中runnable接口也是一个函数式接口,源码如下

@functionalinterface
public interface runnable {
  /**
   * when an object implementing interface <code>runnable</code> is used
   * to create a thread, starting the thread causes the object's
   * <code>run</code> method to be called in that separately executing
   * thread.
   * <p>
   * the general contract of the method <code>run</code> is that it may
   * take any action whatsoever.
   *
   * @see   java.lang.thread#run()
   */
  public abstract void run();
}

将其转换为lambda表达式的写法为

(4)

public void testthread_(){
  new thread(()-> system.out.println("hello, i am thread!")).start();
}

对于没有参数的情况 ,语法:

     1.参数的括号不能省略,如果只有一句的表达式则可省略花括号和语句结尾的分号

我们构造一个只有一个参数的函数式接口

@functionalinterface
public interface myfunctionalinterface {
  public void single(string msg);
}
/**
 * 需要单个参数
 */
public static void testonepar(myfunctionalinterface myfunctionalinterface){
  myfunctionalinterface.single("msg");
}
/**
   * 一个参数,可以省略参数的括号
   */
  @test
  public void testoneparameter(){
    testonepar(x-> system.out.println(x));
  }

对于只有一个参数的情况 ,语法:

     1.参数的括号可以省略

在这里我们为了演示只有一个参数的情况自己创建了一个函数式接口,其实java8中已经为我们提供了很多常见的函数式接口

常见的有

function:提供任意一种类型的参数,返回另外一个任意类型返回值。 r apply(t t);

consumer:提供任意一种类型的参数,返回空值。 void accept(t t);

supplier:参数为空,得到任意一种类型的返回值。t get();

predicate:提供任意一种类型的参数,返回boolean返回值。boolean test(t t);

因此针对上面的情况,我们可以直接使用consumer类,

/**
   * 需要单个参数
   */
  public static void testonepar1(consumer unaryoperator){
    unaryoperator.accept("msg");
  }

总结

以上所述是小编给大家介绍的使用java8之lambda表达式基本语法,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网