Lambda概念介绍

1.Lambda管中窥豹

lambda可以理解成一种匿名内部类的简写方式,行为参数化,他的结构是

参数     箭头   主体

如下图:

package com.three;

import java.util.Comparator;

public class LambdaIntroduce {

    public static void main(String[] args){

        Comparator<Integer> intCompare = new Comparator<Integer>() {
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        };

        //by using Lambda
        Comparator<Integer> intCompareWithLambda = (Integer a1, Integer a2) -> a1.compareTo(a2);

    }
}

 这里a1和a2就是参数,a1.compareTo(a2)就是主体

 Lambda语法有两种表达方式,我的理解就是隐式的return和显式的return.·

1.(parameters) -> expression

  如 (String s1,String s2) -> s1.equals(s2); 

    ()->"Hello"

2.(parameters) ->{statements;}

  如 (String s1,String s2) ->{return s1..equals(s2);}

    ()->{return "Hello";}          

 

2.函数式接口、函数描述符

函数式接口,就是指的有且仅有一个抽象方法的接口

如下:

package com.three;

@FunctionalInterface
public interface FunctionInterface {
    int compare(int t1, int t2);
}

通常会用注解@FunctionalInterface来表示这是一个函数式接口

其中定义的抽象方法就叫做函数描述符

int compare(int t1, int t2);

Lambda只能应用于函数式接口上

 

3.函数式接口的使用

3.1 Predicate<T>

函数描述符是boolean test(T t),表示接受一个泛型,返回boolean值,T->boolean

/*
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package java.util.function;

import java.util.Objects;

/**
 * Represents a predicate (boolean-valued function) of one argument.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #test(Object)}.
 *
 * @param <T> the type of the input to the predicate
 *
 * @since 1.8
 */
@FunctionalInterface
public interface Predicate<T> {

    /**
     * Evaluates this predicate on the given argument.
     *
     * @param t the input argument
     * @return {@code true} if the input argument matches the predicate,
     * otherwise {@code false}
     */
    boolean test(T t);
}

LambdaUseOfPredicate.java:

package com.three;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

public class LambdaUseOfPredicate {

    public static void main(String[] args) {
        List<String> list = Arrays.asList("ab", "ac", "bc", "bd", "");
        Predicate<String> noneEmptyPredicate = (String s) -> !s.isEmpty();
        filter(list, noneEmptyPredicate);

        System.out.println("-------------");

        Predicate<String> startWithAPredicate = (String s) -> s.startsWith("a");
        filter(list, startWithAPredicate);
    }

    /**
     * 过滤并保存满足predicate传递的行为的list中的数据集合
     *
     * @param list
     * @param predicate
     * @param <E>
     * @return
     */
    public static <E> List<E> filter(List<E> list, Predicate<E> predicate) {
        List<E> filterList = new ArrayList<>();
        for (E e : list) {
            if (predicate.test(e)) {
                filterList.add(e);
            }
        }
        for (E e : filterList){
            System.out.print(e);
            System.out.print("   ");
        }
        return filterList;
    }
}
  •   在函数式接口Predicate<T>上,通过使用lambda的语法定义了两个noneEmptyPredicate和startWithAPredicate,可以理解成给抽象方法test做了实现,也说明lambda是应用在函数式接口上的
  •   行为参数化:noneEmpty和startWithA是两种不同的行为,这里当成了filter函数的第二个参数了,这样的好处就是我把我要做的事情包装成了一个参数进入了filter方法,filter里面怎么过滤的,源代码是不用改的,这就是行为参数化,可以简化很多重复逻辑,不然filter需要多次重写

 

3.2 Consumer<T>

   函数描述符是void accept(T t),接受一个泛型对象,返回为空,即T->void,注意这里也是java.util.function下的函数式接口。个人感觉应用就是“纯消费”,只吃不吐,有入参,返回为空

package java.util.function;

import java.util.Objects;

/**
 * Represents an operation that accepts a single input argument and returns no
 * result. Unlike most other functional interfaces, {@code Consumer} is expected
 * to operate via side-effects.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #accept(Object)}.
 *
 * @param <T> the type of the input to the operation
 *
 * @since 1.8
 */
@FunctionalInterface
public interface Consumer<T> {

    /**
     * Performs this operation on the given argument.
     *
     * @param t the input argument
     */
    void accept(T t);

    /**
     * Returns a composed {@code Consumer} that performs, in sequence, this
     * operation followed by the {@code after} operation. If performing either
     * operation throws an exception, it is relayed to the caller of the
     * composed operation.  If performing this operation throws an exception,
     * the {@code after} operation will not be performed.
     *
     * @param after the operation to perform after this operation
     * @return a composed {@code Consumer} that performs in sequence this
     * operation followed by the {@code after} operation
     * @throws NullPointerException if {@code after} is null
     */
    default Consumer<T> andThen(Consumer<? super T> after) {
        Objects.requireNonNull(after);
        return (T t) -> { accept(t); after.accept(t); };
    }
}
LambdaUseOfConsumer.java
package com.three;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class LambdaUseOfConsumer {


    public static void main(String[] args){
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        Consumer<Integer> printElementConsumer = (Integer a) -> System.out.println(a);
        forEach(list, printElementConsumer);

        Consumer<Integer> elementAddOneConsumer = (Integer a) -> {a++;};
        forEach(list, elementAddOneConsumer);
    }

    public static <E> void forEach(List<E> list, Consumer<E> consumer){
        for(E e : list){
            consumer.accept(e);
        }
    }
}

  这个例子用来加深理解行为参数化,这里消费者有两个行为,一个是打印每一个元素,一个是每个元素数值加1,这里把行为当成了函数forEach的参数,即行为参数化,这样子复用了方法forEach而不用根据行为不同而重构.

 

 3.3 Function<T, R>

 函数式接口是Function<T,R>,函数描述符是T->R,即一个泛型转换为另一个泛型

 

package java.util.function;

import java.util.Objects;

/**
 * Represents a function that accepts one argument and produces a result.
 *
 * <p>This is a <a href="package-summary.html">functional interface</a>
 * whose functional method is {@link #apply(Object)}.
 *
 * @param <T> the type of the input to the function
 * @param <R> the type of the result of the function
 *
 * @since 1.8
 */
@FunctionalInterface
public interface Function<T, R> {

    /**
     * Applies this function to the given argument.
     *
     * @param t the function argument
     * @return the function result
     */
    R apply(T t);
}

 

 

 

<think>嗯,用户之前问了匿名函数是什么,现在接着问lambda的用法介绍。这说明他们已经对匿名函数有了基本了解,现在想更具体地学习如何在编程中使用lambda,特别是在不同语言中的应用。 首先,我需要确认lambda在不同编程语言中的具体实现,比如Python、JavaScript、Java、C++等,因为用户之前提到了这些例子。要比较它们的语法差异和使用场景,这样用户能更清楚如何应用。 用户可能希望看到实际例子,这样更容易理解。比如在Python中,lambda常用于sort、map、filter这些高阶函数。需要举一些典型的例子,比如排序列表、处理数据等。 另外,用户可能想知道lambda的限制,比如在Python中lambda只能是一个表达式,不能写多行语句。这时候需要提醒用户注意,当逻辑复杂时应该使用普通函数。 还要注意用户的使用场景,他们可能是刚开始学习编程,或者是在项目中需要快速实现某些功能。所以需要强调lambda的简洁性和临时性,以及何时适合使用,何时不适合。 用户之前的问题里提到了匿名函数的优缺点,所以在lambda的用法介绍里也需要提到这些,比如可读性和维护性的问题,避免嵌套过深。 可能用户对闭包的概念还不熟悉,需要简单解释lambda如何捕获外部变量,但不同语言的处理方式不同,比如Python和JavaScript的区别。 最后,要结构清晰,分点列出不同语言中的用法,比较它们的异同,帮助用户快速查找自己需要的部分。同时,注意事项部分要突出常见错误和最佳实践,确保用户使用时不会遇到太多问题。</think>--- ### **Lambda表达式用法详解** --- #### **一、Lambda的核心概念** **Lambda表达式**是匿名函数的一种简洁实现方式,其核心特点是**省略函数名**,通过**极简语法**直接定义函数逻辑。名称源于数学中的$\lambda$演算(Lambda Calculus),广泛应用于函数式编程和现代语言的高阶函数操作。 --- #### **二、Lambda的通用语法结构** 不同语言中Lambda的语法略有差异,但均遵循以下模式: $$ \text{输入参数} \rightarrow \text{操作或返回值} $$ 例如: - Python:`lambda 参数: 表达式` - JavaScript:`(参数) => { 操作 }` - Java:`(参数) -> { 操作 }` --- #### **三、Python中的Lambda用法** ##### **1. 基础语法** ```python lambda 参数1, 参数2, ... : 表达式 ``` **特点**: - 只能是单行表达式(不能包含`if-else`外的复杂语句) - 自动返回表达式结果(无需`return`) --- ##### **2. 典型应用场景** **场景1:简单运算** ```python add = lambda a, b: a + b print(add(3, 5)) # 输出8 ``` **场景2:配合高阶函数** - **`map`函数**:逐元素处理 ```python numbers = [1, 2, 3] squared = list(map(lambda x: x**2, numbers)) # [1, 4, 9] ``` - **`filter`函数**:条件过滤 ```python even_numbers = list(filter(lambda x: x % 2 == 0, [1, 2, 3, 4])) # [2, 4] ``` - **`sorted`函数**:自定义排序规则 ```python students = [("Alice", 90), ("Bob", 85), ("Charlie", 95)] students_sorted = sorted(students, key=lambda s: s[1]) # 按分数升序排列 ``` **场景3:快速定义回调函数** ```python button.on_click(lambda event: print("点击事件触发")) ``` --- ##### **3. 条件表达式嵌套** 通过三元运算符实现简单分支逻辑: ```python # 判断奇偶 check_odd = lambda x: "奇数" if x % 2 != 0 else "偶数" print(check_odd(7)) # 输出"奇数" ``` --- #### **四、JavaScript中的Lambda(箭头函数)** ##### **1. 基础语法** ```javascript (参数) => { 操作 } // 或单行隐式返回 (参数) => 表达式 ``` **示例**: ```javascript const multiply = (a, b) => a * b; console.log(multiply(2, 3)); // 输出6 ``` --- ##### **2. 关键特性** - **`this`绑定**:箭头函数不绑定自身`this`,继承外层上下文 ```javascript function Timer() { this.seconds = 0; setInterval(() => { this.seconds++; // 正确访问外层this }, 1000); } ``` - **简化回调** ```javascript [1, 2, 3].map(x => x * 2); // 返回[2, 4, 6] ``` --- #### **五、Java中的Lambda** ##### **1. 语法规则** ```java (参数列表) -> { 操作 } ``` **限制**:必须匹配函数式接口(如`Runnable`, `Comparator`)。 --- ##### **2. 应用示例** **示例1:线程任务** ```java new Thread(() -> System.out.println("线程运行")).start(); ``` **示例2:集合排序** ```java List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); names.sort((s1, s2) -> s1.length() - s2.length()); ``` --- #### **六、C++中的Lambda** ##### **1. 语法结构** ```cpp [捕获列表](参数) -> 返回类型 { 函数体 } ``` **示例**: ```cpp auto sum = [](int a, int b) { return a + b; }; cout << sum(3, 4); // 输出7 ``` --- ##### **2. 捕获模式** - **值捕获**:`[x]`(复制外部变量值) - **引用捕获**:`[&x]`(直接操作外部变量) - **混合捕获**:`[=, &y]`(默认值捕获,仅y引用捕获) --- #### **七、Lambda的注意事项** | 问题类型 | 说明与解决方案 | |------------------|-------------------------------------------------------------------------------| | **可读性差** | 避免多层嵌套Lambda(如`map`内嵌`filter`),改用普通函数或拆分步骤 | | **调试困难** | Lambda无函数名,调试时需通过上下文定位问题 | | **语言限制** | Python的Lambda不支持多语句;Java需匹配函数式接口 | | **变量作用域** | 注意闭包中外部变量的生命周期(如C++引用捕获可能导致悬空引用) | --- #### **八、最佳实践** 1. **适用场景** - 简单逻辑(如单行表达式) - 临时性操作(如排序规则、事件回调) 2. **避免场景** - 复杂业务逻辑(需拆分为普通函数) - 高频调用的性能关键代码(可能影响效率) --- #### **总结** Lambda表达式通过**简化函数定义**大幅提升代码紧凑性,尤其适用于函数式编程和高阶函数场景。但其简洁性需与可维护性平衡,建议在明确需求且逻辑简单时优先使用,复杂场景仍推荐传统函数定义方式。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值