杂项-Maven-guava:guava
| ylbtech-杂项-Maven-guava:guava |
| 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)
让我们来了解上述程序的一些重要概念。
Optional - 实用类,使代码使用null能够正常。
Optional.of - 返回要用作参数Optional类的实例。检查传递的值是否为null。
Optional.get - 获取输入存储在Optional 类的值。
使用Optional类,可以方便地查看调用者方法来传递参数正确与否。
| 2.返回顶部 |
| 3.返回顶部 |
| 4.返回顶部 |
| 5.返回顶部 |
| 6.返回顶部 |
![]() |
作者:ylbtech 出处:http://ylbtech.cnblogs.com/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
杂项-Maven-guava:guava的更多相关文章
- SpringBoot 遇到 com.google.guava » guava 组件运行异常问题修复方案
环境 Apache Maven : 3.5.4 org.springframework.boot » spring-boot-starter-parent : 2.0.3.RELEASE io.spr ...
- Google Java编程库Guava介绍
本系列想介绍下Java下开源的优秀编程库--Guava[ˈgwɑːvə].它包含了Google在Java项目中使用一些核心库,包含集合(Collections),缓存(Caching),并发编程库(C ...
- java开发人员,最应该学习和熟练使用的工具类。google guava.(谷歌 瓜娃)
学习参考文章: http://blog.csdn.net/wisgood/article/details/13297535 http://ifeve.com/google-guava/ http:// ...
- guava常用操作
Jack47 我思故我在 Google Java编程库Guava介绍 本系列想介绍下Java下开源的优秀编程库--Guava[ˈgwɑːvə].它包含了Google在Java项目中使用一些核心库,包含 ...
- Google Guava之--cache
一.简介 Google Guava包含了Google的Java项目许多依赖的库,如:集合 [collections] .缓存 [caching] .原生类型支持 [primitives support ...
- Guava库
Guava是一个非常棒的库,它就是Java标准库"所缺失的那部分",是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, ...
- Google的Guava类库简介(转)
说明:信息虽然有点旧,至少可以先了解个大概. Guava是一个Google的基于Java的类库集合的扩展项目,包括collections, caching, primitives support, c ...
- Guava -- 集合类 和 Guava Cache
Guava -- 集合类 和 Guava Caches 1. 什么是 Guava Guava 是 google 推出的一个第三方 java 库,用来代替 jdk 的一些公共操作,给我印象特别深的就是 ...
- Guava入门使用教程
Guava入门使用教程 Guava Maven dependency In our examples, we use the following Maven dependency. <depen ...
随机推荐
- LGV定理 (CodeForces 348 D Turtles)/(牛客暑期多校第一场A Monotonic Matrix)
又是一个看起来神奇无比的东东,证明是不可能证明的,这辈子不可能看懂的,知道怎么用就行了,具体看wikihttps://en.wikipedia.org/wiki/Lindstr%C3%B6m%E2%8 ...
- Vue登录登出以及JWT认证
数据模型 主要用户名,密码,邮箱,头像,身份 const mongoose = require('mongoose') const schema = new mongoose.Schema({ use ...
- NSDateFormatter 今年日期格式化成字符串是明年日期问题?
在项目里我要是把NSDate格式化成字符串 我的format是@"YYYY年MM月dd日 HH:mm" 传入日期2013-12-30 15:00:00后,返回给我的字符串是 201 ...
- 编译器报错: error LNK2001: unresolved external symbol "struct _ServiceDescriptorTable * KeServiceDescript
转自VC错误:http://www.vcerror.com/?p=10 问题描述: 编译器报错: error LNK2001: unresolved external symbol "str ...
- 专题:“find -perm”
Search for files which have read and write permission for their owner, and group, but which other us ...
- 前端开发者进阶之ECMAScript新特性--Object.create
前端开发者进阶之ECMAScript新特性[一]--Object.create Object.create(prototype, descriptors) :创建一个具有指定原型且可选择性地包含指 ...
- Spring Boot Server容器配置
参数配置容器 server.xx开头的是所有servlet容器通用的配置,server.tomcat.xx开头的是tomcat特有的参数,其它类似. 所有参数绑定配置类:org.springframe ...
- 天道神诀---DHCP服务(下篇)
DHCP作用域详解 subnet 定义一个作用域 netmask 定义作用域的掩码 range 允许发放的IP范围 option routers 指定网关地址 option domain-nam ...
- Flutter 打包报错 : Unknown FLUTTER_BUILD_MODE: xxx
概要 在集成flutter 工程之后,我们的工程在debug 和release 模式下都没什么问题,一切都很顺利.但是我们在打企业包的时候却出现了错误: Showing Recent Errors O ...
- 《软件调试修炼之道》Part 1(CH1~5)读书笔记 PB16110698 第八周(~4.26)
编程中,调试几乎是必不可少的,一劳永逸.一次完成预想功能而完全不出bug的情况凤毛麟角,出现bug→调试→再出现bug→再调试……基本是软件工程中的常态.可以说,软件调试是每个coder的必修课,而& ...
