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));
    
        }
    }
    

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

    作者:吕世昊

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

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

  • 相关阅读:
    Android10_内容提供者_内容观察者_标题栏
    Android08_广播接受者_服务
    Android09_远程服务_系统服务_aidl
    Android07_多界面_Activity生命周期
    Android06_getpost提交_文件上传_多线程下载
    Android_handler_网络请求_img框架
    Android开发04
    java 正则表达式验证邮箱
    postman 基本用法
    java 微信申请退款的开发
  • 原文地址:https://www.cnblogs.com/lvshihao/p/15162753.html
Copyright © 2011-2022 走看看