zoukankan      html  css  js  c++  java
  • Java-类库:guava

    ylbtech-Java-类库:guava
    Guava是一种基于开源的Java库,其中包含谷歌正在由他们很多项目使用的很多核心库。这个库是为了方便编码,并减少编码错误。这个库提供用于集合,缓存支持原语,并发性,常见注解,字符串处理,I/O和验证的实用方法
    1.返回顶部
    1、

    Guava 是什么?

    Guava是一种基于开源的Java库,其中包含谷歌正在由他们很多项目使用的很多核心库。这个库是为了方便编码,并减少编码错误。这个库提供用于集合,缓存,支持原语,并发性,常见注解,字符串处理,I/O和验证的实用方法。

    Guava的好处

    • 标准化 - Guava库是由谷歌托管。
    • 高效 - 可靠,快速和有效的扩展JAVA标准库
    • 优化 -Guava库经过高度的优化。

    函数式编程 -增加JAVA功能和处理能力。

    实用程序 - 提供了经常需要在应用程序开发的许多实用程序类。

    验证 -提供标准的故障安全验证机制。

    最佳实践 - 强调最佳的做法。

    考虑下面的代码片段。

    public class GuavaTester {
       public static void main(String args[]){
          GuavaTester guavaTester = new GuavaTester();
          Integer a =  null;
          Integer b =  new Integer(10);
          System.out.println(guavaTester.sum(a,b));
       }
    
       public Integer sum(Integer a, Integer b){
          return a + b;
       }    
    }

    运行程序,看到如下结果。

    Exception in thread "main" java.lang.NullPointerException
        at GuavaTester.sum(GuavaTester.java:13)
        at GuavaTester.main(GuavaTester.java:9)

    以下是该代码的问题。

    sum() 不采取任何的保护传递的参数为null。

    调用函数也并不担心传递一个null 到sum()方法而产生意外。

    当程序运行时,NullPointerException异常发生。

    为了避免上述问题,null检查要在每个这样存在问题地方。

    让我们来看看使用Optional,Guava 提供实用工具类来标准化方式解决上述问题。

    import com.google.common.base.Optional;
    
    public class GuavaTester {
       public static void main(String args[]){
          GuavaTester guavaTester = new GuavaTester();
    
          Integer invalidInput = null;
          Optional<Integer> a =  Optional.of(invalidInput);
          Optional<Integer> b =  Optional.of(new Integer(10));
          System.out.println(guavaTester.sum(a,b));
       }
    
       public Integer sum(Optional<Integer> a, Optional<Integer> b){
          return a.get() + b.get();
       }    
    }

    运行程序,看到结果如下。

    Exception in thread "main" java.lang.NullPointerException
        at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:210)
        at com.google.common.base.Optional.of(Optional.java:85)
        at GuavaTester.main(GuavaTester.java:8)
    Java

    让我们来了解上述程序的一些重要概念。

    Optional - 实用类,使代码使用null能够正常。

    Optional.of - 返回要用作参数Optional类的实例。检查传递的值是否为null。

    Optional.get - 获取输入存储在Optional 类的值。

    使用Optional类,可以方便地查看调用者方法来传递参数正确与否。

    2、
    2.返回顶部
     
    3.返回顶部
     
    4.返回顶部
     
    5.返回顶部
    1、
    2、中文教程
    3、
     
    6.返回顶部
     
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Compression algorithm (deflate)
    tcpip数据包编码解析(chunk and gzip)_space of Jialy_百度空间
    What's the difference between the "gzip" and "deflate" HTTP 1.1 encodings?
    gzip压缩算法: gzip 所使用压缩算法的基本原理
    Decompressing a GZip Stream with Zlib
    Frequently Asked Questions about zlib
    how to decompress gzip stream with zlib
    自己动手写web服务器四(web服务器是如何通过压缩数据,web服务器的gzip模块的实现)
    What's the difference between the "gzip" and "deflate" HTTP 1.1 encodings?
    C语言抓http gzip包并解压 失败 C/C++ ChinaUnix.net
  • 原文地址:https://www.cnblogs.com/storebook/p/11023692.html
Copyright © 2011-2022 走看看