首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 8 Function 函数接口

Java 8 Function 函数接口

作者头像
未读代码
发布2021-08-12 14:51:14
6630
发布2021-08-12 14:51:14
举报

封面图:绍兴 · 三味书屋(2021-07-10)

在 Java 8 中,Function 接口是一个函数接口,它位于包 java.util.function 下。Function 接口中定义了一个 R apply(T t) 方法,它可以接受一个泛型 T 对象,返回一个泛型 R 对象,即参数类型和返回类型可以不同。

Function 接口源码:

@FunctionalInterface
public interface Function<T, R> {

    R apply(T t);

    default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
        Objects.requireNonNull(before);
        return (V v) -> apply(before.apply(v));
    }

    default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
        Objects.requireNonNull(after);
        return (T t) -> after.apply(apply(t));
    }

    static <T> Function<T, T> identity() {
        return t -> t;
    }
}

1. Function apply

示例 1:输入一个字符串 <T> String, 返回字符串的大写形式 <R> String

package com.wdbyte;

import java.util.function.Function;

public class Java8Function {
    public static void main(String[] args) {
        Function<String, String> toUpperCase = str -> str.toUpperCase();
        String result = toUpperCase.apply("www.wdbyte.com");
        System.out.println(result);
    }
}

输出结果:

WWW.WDBYTE.COM

示例 2:输入一个字符串 <T> String,返回字符串的长度 <R> Integer

package com.wdbyte;

import java.util.function.Function;

public class Java8FunctionLength {
    public static void main(String[] args) {
        Function<String, Integer> lengthFunction = str -> str.length();
        Integer length = lengthFunction.apply("www.wdbyte.com");
        System.out.println(length);
    }
}

输出结果:

14

2. Function andThen

Function 函数接口的 andThen() 方法可以让多个 Function 函数连接使用。

示例:输入一个字符串,获取字符串的长度,然后乘上 2。

package com.wdbyte;

import java.util.function.Function;

public class Java8FunctionAndThen {
    public static void main(String[] args) {
        Function<String, Integer> lengthFunction = str -> str.length();
        Function<Integer, Integer> doubleFunction = length -> length * 2;
        Integer doubleLength = lengthFunction.andThen(doubleFunction).apply("www.wdbyte.com");
        System.out.println(doubleLength);
    }
}

结果:

28

3. List -> Map

示例:输入一个字符串 List 集合<T> List<String> , 返回一个 key 为字符串本身,Value 为字符串长度的 Map

package com.wdbyte;

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

public class Java8FunctionListToMap {

    public static void main(String[] args) {
        List<String> list = Arrays.asList("java", "nodejs", "wdbyte.com");
        // lambda 方式
        Function<String, Integer> lengthFunction = str -> str.length();
        Map<String, Integer> listToMap = listToMap(list, lengthFunction);
        System.out.println(listToMap);

        // 方法引用方式
        Map<String, Integer> listToMap2 = listToMap(list, String::length);
        System.out.println(listToMap2);
    }

    public static <T, R> Map<T, R> listToMap(List<T> list, Function<T, R> function) {
        HashMap<T, R> hashMap = new HashMap<>();
        for (T t : list) {
            hashMap.put(t, function.apply(t));
        }
        return hashMap;
    }
}

输出结果:

{java=4, wdbyte.com=10, nodejs=6}
{java=4, wdbyte.com=10, nodejs=6}

4. List -> List<Other>

示例 :输入一个字符串 List 集合 <T> List<String> ,返回大写形式的字符串 List 集合,返回小写形式的字符串 List 集合。

package com.wdbyte;

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

public class Java8FunctionString {

    public static void main(String[] args) {
        List<String> list = Arrays.asList("Java", "Nodejs", "Wdbyte.com");
        // 方法引用方式
        List<String> upperList = map(list, String::toUpperCase);
        List<String> lowerList = map(list, String::toLowerCase);
        System.out.println(upperList);
        System.out.println(lowerList);

        // Lambda 方式
        List<String> upperList2 = map(list, x -> x.toUpperCase());
        List<String> lowerList2 = map(list, x -> x.toLowerCase());
        System.out.println(upperList2);
        System.out.println(lowerList2);

    }

    public static <T, R> List<R> map(List<T> list, Function<T, R> function) {
        List<R> resultList = new ArrayList<>(list.size());
        for (T t : list) {
            resultList.add(function.apply(t));
        }
        return resultList;
    }
}

输出结果:

[JAVA, NODEJS, WDBYTE.COM]
[java, nodejs, wdbyte.com]
[JAVA, NODEJS, WDBYTE.COM]
[java, nodejs, wdbyte.com]
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-07-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 程序猿阿朗 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. Function apply
  • 2. Function andThen
  • 3. List -> Map
  • 4. List -> List<Other>
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档