自学内容网 自学内容网

java.util.function Function<T, R>

一、介绍

1、简介

Function<T, R> 是 Java 8 中的一个函数式接口,用于表示接受一个输入参数 T,并返回一个结果 R 的函数。Function接口中有一个抽象方法apply,用于定义函数的逻辑。Function接口通常用于将数据进行转换(处理逻辑由Lambda表达式实现)、映射或者执行某种转换操作。

2、常用的方法

R apply(T t):将此函数应用于给定的参数

default<V> Function andThen(Function after): 返回一个组合函数,首先将该函数应用于输入,然后将after函数应用于结果

二、用法

1、简单使用
import java.util.function.Function;

public class FunctionExample1 {
    public static void main(String[] args) {
        // 示例1:将字符串转换为对应的整数
        Function<String, Integer> strToInt = Integer::parseInt;
        int num = strToInt.apply("123");
        System.out.println(num); // 输出: 123

        // 示例2:将字符串转换为其长度
        Function<String, Integer> strLength = String::length;
        int length = strLength.apply("Hello World");
        System.out.println(length); // 输出: 11

        // 示例3:组合多个函数
        Function<String, Integer> strToIntAndMultiplyBy2 = strToInt.andThen(n -> n * 2);
        int result = strToIntAndMultiplyBy2.apply("5");
        System.out.println(result); // 输出: 10
    }
}
2、Function接口作为方法的参数

用于将函数作为参数传递

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

public class FunctionExample2 {
    public static void main(String[] args) {
        // 示例1:将Function作为方法参数
        int result1 = calculate(5, num -> num * 2);
        System.out.println(result1); // 输出:10

        // 示例2:将Function作为方法返回值
        Function<Integer, Integer> multiplier = getMultiplier();
        int result2 = multiplier.apply(5);
        System.out.println(result2); // 输出:8

        // 示例3:使用Function接口作为方法参数或返回值
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        Function<Integer, String> intToString2 = (num) -> String.valueOf(num);

        processList(numbers, intToString2); // 将整数列表转换为字符串并打印出来
    }

    // 方法接受一个整数和一个Function作为参数,将整数应用到Function上并返回结果
    public static int calculate(int num, Function<Integer, Integer> operation) {
        return operation.apply(num);
    }

    // 方法返回一个Function,用于将整数加上固定值
    public static Function<Integer, Integer> getMultiplier() {
        int multiplierValue = 3;
        return num -> num + multiplierValue;
    }

    // 方法接受一个整数列表和一个将整数转换为字符串的函数作为参数
    // 它遍历整数列表,对每个整数应用传入的函数,将结果打印出来
    public static void processList(List<Integer> list, Function<Integer, String> processor) {
        for (Integer num : list) {
            String result = processor.apply(num);
            System.out.println(result);
        }
    }
}
3、Function接口作为方法的返回值

作为结果返回

public static Function<String, String> PING_PONG_INTERVAL = value -> {
        try {
            int number = Integer.parseInt(value);
            if (number <= 0) {
                throw new IllegalArgumentException("the value must be a number greater than 0.");
            }
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("the value must be a number.");
        }
        return value;
    };
 public  static  Function<String, String> RANGE_NUMBER(Range<Long> range) {
        return value -> {
            try {
                long number = Long.parseLong(value);
                if (!range.contains(number)) {
                    long lowerValue = range.lowerEndpoint();
                    long upperValue = range.upperEndpoint();
                    throw new NumberRangeException("the value must be a number range "+lowerValue+" to "+upperValue);
                }
            } catch (NumberFormatException e) {
                throw new NumberRangeException("the value must be a number.");
            }
            return value;
        };
    }
 public Function<String,UserDTO> getUserFunction = userId-> {
        UserDTO user= new UserDTO();
        user.setId(IDGenerator.getId());
        user.setUserId(userId);
        return user;
    };


原文地址:https://blog.csdn.net/w_t_y_y/article/details/142757958

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!