zoukankan      html  css  js  c++  java
  • Java 8 Function 函数接口

    点赞再看,动力无限。Hello world : ) 微信搜「 程序猿阿朗 」。

    本文 Github.com/niumoo/JavaNotes未读代码博客 已经收录,有很多知识点和系列文章。

    这篇文章属于 Java 8 教程(LTS)系列教程

    在 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]
    

    参考

    扩展阅读

    订阅

    <完>

    Hello world : ) 我是阿朗,一线技术工具人,认认真真写文章。

    点赞的个个都是人才,不仅长得帅气好看,说话还好听。


    文章持续更新,可以关注公众号「 程序猿阿朗 」或访问「未读代码博客 」。

    回复【资料】有我准备的各系列知识点和必看书籍。

    本文 Github.com/niumoo/JavaNotes 已经收录,有很多知识点和系列文章,欢迎Star。

    等你好久

  • 相关阅读:
    ZOJ 2588 Burning Bridges
    POJ 1966 ZOJ 2182 Cable TV Network
    HDU 5348 MZL's endless loop
    HDU 5352 MZL's City
    Tarjan算法求解无向连通图的割点、割边、点双连通分量和边双连通分量的模板
    ZOJ 1119 SPF
    HDU 3452 Bonsai
    HDU 1520 Anniversary party
    POJ 2239 Selecting Courses
    POJ 1144 Network
  • 原文地址:https://www.cnblogs.com/niumoo/p/15034420.html
Copyright © 2011-2022 走看看