zoukankan      html  css  js  c++  java
  • Guava入门第六章(Functions)

    Functions详细介绍


    package com.lvshihao.guava;
    
    import com.google.common.base.*;
    
    /**
     *@author: LVSHIHAO
     *@description: GUAVA Function detailed introduction
     * 和 JAVA 1.8 的 lambda 表达式类似
     */
    public class FunctionsTest {
    
        public static void main(String[] args) {
            //define The Method Logic Of A Functional Interface
            Function<String,Integer> function=(v->{
                Preconditions.checkNotNull(v);
                return v.length();
            });
    
            System.out.println(function.apply("lvshihao"));
            process("lvshihao",new Handler.LengthDoubleHandler());
            System.out.println(Functions.toStringFunction().apply(new Object()));
    
            /**
             * use Guava Functions Functional Interface
             */
            Function<String,Double> compose=Functions.compose((Integer v1)->v1*3.0,(String input)->input.length());
            Double composeValue = compose.apply("lvshihao");
            System.out.println(composeValue);
    
            /**
             * use Guava Suppliers Functional Interface
             */
            Supplier<String> compose1 = Suppliers.compose(v -> v+"eee", () -> 6666);
            String s = compose1.get();
            System.out.println(s);
        }
        interface Handler<IN,OUT>{
            OUT handler(IN input);
    
            class LengthDoubleHandler implements Handler<String,Integer>{
    
                @Override
                public Integer handler(String input) {
                    return input.length()*2;
                }
            }
        }
    
        private static void process(String text,Handler<String,Integer> handler){
    
            System.out.println(handler.handler(text));
    
        }
    }
    

    ---------------------------------------------------------------------------------------------------------------------------------------------------------------------

    作者:吕世昊

    个性签名:学习如逆水行舟,不进则退!

    如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

  • 相关阅读:
    代理模式(Proxy)
    桥接模式(Bridge)
    组合模式(Composite)
    装饰器模式(Decorator)
    外观模式(Facade)
    享元模式(FlyWeight)
    职责链模式(Chain of Responsibility)
    迭代器模式(Iterator)
    中介者模式(Mediator)
    命令模式(Command)
  • 原文地址:https://www.cnblogs.com/lvshihao/p/15162753.html
Copyright © 2011-2022 走看看