Java Lambda表达式:为什么要使用它们?

Java Lambda表达式:为什么要使用它们?,java,android,lambda,Java,Android,Lambda,我听说这是一个在C#中名为Lambda表达式的概念,它是在Java8中添加的。 我是安卓开发者,所以我关心的是为什么这个概念会添加到Java中。当然,它有一个很好的优势,但我没有发现这一点 我将其添加到我的项目中: android { compileSdkVersion 23 buildToolsVersion '24.0.0' defaultConfig { applicationId "com.example.mytest" minSd

我听说这是一个在C#中名为Lambda表达式的概念,它是在Java8中添加的。 我是安卓开发者,所以我关心的是为什么这个概念会添加到Java中。当然,它有一个很好的优势,但我没有发现这一点

我将其添加到我的项目中:

android {
    compileSdkVersion 23
    buildToolsVersion '24.0.0'
    defaultConfig {
        applicationId "com.example.mytest"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        //for using java 8 and it's features like lambda expressions
        jackOptions { enabled true }


    }

 compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }
写一些我在谷歌上搜索到的Lambda表达式:

button4.setOnClickListener( e ->  Toast.makeText(Main2Activity.this, "test", Toast.LENGTH_SHORT).show());

我真的锁定了一个在android中使用Lambda表达式的好例子。

Java-8中的Lambda表达式做不了什么,旧Java做不了什么。唯一的区别是您需要更少的键入,代码变得更具可读性。(我一直讨厌那些匿名类)

好吧,Java-8中的Lambda表达式做不了什么,旧Java做不了什么。唯一的区别是您需要更少的键入,代码变得更具可读性。(我一直讨厌那些匿名类)

带来Lambdas允许以函数式风格编写代码。对于某些领域(如业务逻辑),这是一个巨大的优势——可以产生更紧凑、更可读的代码

在Java8中,添加了一个新的。Lambdas是该扩展的一部分


.

引入Lambdas允许以函数式风格编写代码。对于某些领域(如业务逻辑),这是一个巨大的优势——可以产生更紧凑、更可读的代码

在Java8中,添加了一个新的。Lambdas是该扩展的一部分


.

Lambda表达式只是添加到java中,以使您的代码模式具有表达能力

。在这个过程中,它们简化了某些常见构造的实现方式

lambda的加法 表达式还为其他新的Java特性(包括默认特性)提供了催化剂 方法,该方法允许您定义接口的默认行为 方法,以及方法引用(此处描述),它允许您引用方法 没有执行它

所以lambda表达式不会对android的编码方式做出任何改变,它只是增强了java的编码方式

您需要声明函数接口才能使用lambda表达式

一个小例子

// A functional interface. 
interface MyNumber { 
double getValue(); 
} 

class LambdaDemo { 
 public static void main(String args[]) 
 { 
MyNumber myNum;  // declare an interface reference 

// Here, the lambda expression is simply a constant expression. 
// When it is assigned to myNum, a class instance is 
// constructed in which the lambda expression implements 
// the getValue() method in MyNumber. 
myNum = () -> 123.45; 

  // Call getValue(), which is provided by the previously assigned 
// lambda expression. 
System.out.println("A fixed value: " + myNum.getValue()); 

// Here, a more complex expression is used. 
myNum = () -> Math.random() * 100; 

// These call the lambda expression in the previous line. 
System.out.println("A random value: " + myNum.getValue()); 
System.out.println("Another random value: " + myNum.getValue()); 

// A lambda expression must be compatible with the method 
// defined by the functional interface. Therefore, this won't work: 
//  myNum = () -> "123.03"; // Error! 
} 
 }
**程序的示例输出**

A fixed value: 123.45 
A random value: 88.90663650412304 
Another random value: 53.00582701784129

Lambda表达式只是添加到java中,以使代码模式具有表达能力

。在这个过程中,它们简化了某些常见构造的实现方式

lambda的加法 表达式还为其他新的Java特性(包括默认特性)提供了催化剂 方法,该方法允许您定义接口的默认行为 方法,以及方法引用(此处描述),它允许您引用方法 没有执行它

所以lambda表达式不会对android的编码方式做出任何改变,它只是增强了java的编码方式

您需要声明函数接口才能使用lambda表达式

一个小例子

// A functional interface. 
interface MyNumber { 
double getValue(); 
} 

class LambdaDemo { 
 public static void main(String args[]) 
 { 
MyNumber myNum;  // declare an interface reference 

// Here, the lambda expression is simply a constant expression. 
// When it is assigned to myNum, a class instance is 
// constructed in which the lambda expression implements 
// the getValue() method in MyNumber. 
myNum = () -> 123.45; 

  // Call getValue(), which is provided by the previously assigned 
// lambda expression. 
System.out.println("A fixed value: " + myNum.getValue()); 

// Here, a more complex expression is used. 
myNum = () -> Math.random() * 100; 

// These call the lambda expression in the previous line. 
System.out.println("A random value: " + myNum.getValue()); 
System.out.println("Another random value: " + myNum.getValue()); 

// A lambda expression must be compatible with the method 
// defined by the functional interface. Therefore, this won't work: 
//  myNum = () -> "123.03"; // Error! 
} 
 }
**程序的示例输出**

A fixed value: 123.45 
A random value: 88.90663650412304 
Another random value: 53.00582701784129

Lambdas删除了样板代码,使应用程序更加全面。尤其是当您使用RxJava库时,这是正确的-如果应用程序以反应式方式工作,您需要在单独的链块中定义每个操作,没有lambdas看起来非常笨拙。

lambdas删除样板代码,使应用程序更全面。尤其是当您使用RxJava库时——如果应用程序以反应式方式工作,您需要在单独的链块中定义每个操作,没有lambda看起来非常笨拙。

好吧,lambda表达式就像Java几十年前的可调用或可运行接口

Java 8带来了一些接口约定,这些约定将由lambda表达式实现,这是实现中最常见的用法

 Void=Consumer
 Boolean=Predicate
 Send/Return argument=Function
这将是一个函数函数示例

           /**
 * In this example we use a Function, which receive an item and then return the same or another item through the pipeline.
 * Is the function used by mutable operators as Map or FlatMap
 *
 */
@Test
public void functionFunction() throws InterruptedException {
    String words = Stream.of("hello_functional_world")
                         .map(replaceWordsFunction())
                         .reduce("", String::concat);

    System.out.println(words);
}

private Function<String, String> replaceWordsFunction() {
    return a -> a.replace("_", " ");
}
/**
*在本例中,我们使用一个函数,它接收一个项目,然后通过管道返回相同或另一个项目。
*可变运算符使用的函数是Map还是FlatMap
*
*/
@试验
public void function()引发InterruptedException{
stringwords=Stream.of(“hello\u functional\u world”)
.map(replaceWordsFunction())
.reduce(“,字符串::concat);
System.out.println(字);
}
私有函数replaceWordsFunction(){
返回a->a.replace(““,”);
}
您可以查看这些实际示例,详细说明此新功能的工作原理,并使您的世界成为一个更好的居住场所


好吧,lambda表达式就像一个可调用或可运行的接口,几十年前就已经在Java中出现了

Java 8带来了一些接口约定,这些约定将由lambda表达式实现,这是实现中最常见的用法

 Void=Consumer
 Boolean=Predicate
 Send/Return argument=Function
这将是一个函数函数示例

           /**
 * In this example we use a Function, which receive an item and then return the same or another item through the pipeline.
 * Is the function used by mutable operators as Map or FlatMap
 *
 */
@Test
public void functionFunction() throws InterruptedException {
    String words = Stream.of("hello_functional_world")
                         .map(replaceWordsFunction())
                         .reduce("", String::concat);

    System.out.println(words);
}

private Function<String, String> replaceWordsFunction() {
    return a -> a.replace("_", " ");
}
/**
*在本例中,我们使用一个函数,它接收一个项目,然后通过管道返回相同或另一个项目。
*可变运算符使用的函数是Map还是FlatMap
*
*/
@试验
public void function()引发InterruptedException{
stringwords=Stream.of(“hello\u functional\u world”)
.map(replaceWordsFunction())
.reduce(“,字符串::concat);
System.out.println(字);
}
私有函数replaceWordsFunction(){
返回a->a.replace(““,”);
}
您可以查看这些实际示例,详细说明此新功能的工作原理,并使您的世界成为一个更好的居住场所

我终于发现了这一点:

Lambda表达式是在Java8中引入的,并被吹捧为Java8的最大特性。Lambda表达式简化了函数式编程,大大简化了开发过程

lambda表达式具有以下语法特征−

param