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类,可以方便地查看调用者方法来传递参数正确与否。


773阅读 | 0评论
你的回应
  • amuyuezuwu

    2019-06-13 14:17:45 amuyuezuwu

    Ewing's tfb.bbwr.osbaike.net.jyj.ds headaches, coarse, [URL=http://vintagepowderpuff.com/cash-advance/]cash advance[/URL] [URL=http://bootstrapplusplus.com/argumentative-essay/]politics essay[/URL] [URL=http://circulateindia.com/same-day-loans/]same day loans[/URL] [URL=http://thegrizzlygrowler.com/essay-writer/]explanatory synthesis essay[/URL] [URL=http://bootstrapplusplus.com/write-my-essay-for-me/]obesity essay[/URL] [URL=http://myquickrecipes.com/personal-loan/]payday loans no credit check[/URL] dry sulphate,

  • uhoiyoluxezip

    2019-06-03 20:07:58 uhoiyoluxezip

    C7 jrx.hjbr.osbaike.net.xyn.nd hundreds [URL=http://anguillacayseniorliving.com/viagra-com/]viagra[/URL] [URL=http://anguillacayseniorliving.com/cialis-online/]subaction showcomments cialis thanks online[/URL] [URL=http://washingtonsharedparenting.com/renova--for-sale/]renova[/URL] [URL=http://anguillacayseniorliving.com/buy-cialis/]cialis price[/URL] [URL=http://tofupost.com/cialis/]tadalafil tablets 20 mg[/URL] [URL=http://tacticaltomahawkreviews.com/valtrex/]valtrex online[/URL] [URL=http://pintlersuites.com/viagra-professional/]viagra professional for sale[/URL] %5

  • adeneru

    2019-05-30 14:14:42 adeneru

    D; ilg.mbom.osbaike.net.xjc.lv surrounds [URL=http://anguillacayseniorliving.com/prednisone/]prednisone 20 mg side effects[/URL] buy prednisone without prescription [URL=http://gaiaenergysystems.com/buy-accutane-online/]accutane on line[/URL] [URL=http://foodfhonebook.com/sildalis/]sildalis canada[/URL] [URL=http://washingtonsharedparenting.com/ventolin-inhaler/]adimmix ventolin inhaler online[/URL] [URL=http://foodfhonebook.com/brand-viagra/]brand viagra for sale[/URL] brand viagra without a prescription [URL=http://infaholic.com/propecia-online/]propecia finasteride[/URL] finasteride no prescription [URL=http://anguillacayseniorliving.com/buy-viagra/]viagra generic[/URL] [URL=http://gaiaenergysystems.com/viagra-pills/]lowest price for viagra 100mg[/URL] tear's delicate by prednisone w not prescription accutane online sildalis ventolin inhaler without a prescription brand viagra without a prescription buy finasteride online viagra for cheap ajanta kamagra concerned, started http://anguillacayseniorliving.com/prednisone/#prednisone prednisone online prednisone no presciption http://gaiaenergysystems.com/buy-accutane-online/#generic-accutane accutane online http://foodfhonebook.com/sildalis/#sildalis-lowest-price sildalis online http://washingtonsharedparenting.com/ventolin-inhaler/#online-ventolin-inhaler spanish instructions ventolin inha

  • uxawielirahit

    2019-05-22 07:12:09 uxawielirahit

    Venous fmr.icmw.osbaike.net.lhp.do plotting practitioners fundus [URL=http://gccroboticschallenge.com/generic-cialis/]tadalafil 20mg[/URL] generic cialis tadalafil [URL=http://scoverage.org/buy-cialis/]buy cialis on line[/URL] [URL=http://a1sewcraft.com/cialis-on-line/]cialis 20 mg lowest price[/URL] [URL=http://ekautorepair.com/accutane/]buy accutane online[/URL] [URL=http://a1sewcraft.com/discount-viagra/]viagra generic[/URL] viagra generic [URL=http://discoveryshows.com/lyrica/]cheapest lyrica[/URL] [URL=http://columbiainnastoria.com/buy-cialis-online/]c

  • otavacyuto

    2019-05-21 01:30:39 otavacyuto

    Discussion jao.puep.osbaike.net.obd.sj encapsulated; [URL=http://azlyricsall.com/prednisone/]prednisone without dr prescription[/URL] [URL=http://wyovacationrental.com/viagra-canada/]viagra[/URL] [URL=http://irc305.com/online-pharmacy/]cialis canadian pharmacy[/URL] [URL=http://oliveogrill.com/generic-cialis-lowest-price/]gunstig cialis kaufen[/URL] [URL=https://pharmacy24h.wixsite.com/levitra/]levitra[/URL] asparagus, happen, monomer prednisone without an rx lowest pri

  • adeluxitekefi

    2019-05-20 06:31:21 adeluxitekefi

    May ysi.pzoq.osbaike.net.hbl.eu tolerate amorphous contusion [URL=http://scoverage.org/buy-viagra/]viagra[/URL] viagra [URL=http://breakwaterfamily.com/prednisone/]buy prednisone without a prescription[/URL] [URL=http://michiganvacantproperty.org/generic-levitra/]online levitra[/URL] [URL=http://a1sewcraft.com/prednisone-without-dr-prescription/]prednisone without dr prescription[/URL] [URL=http://davincipictures.com/buy-accutane-online/]buy accutane online[/URL] [URL=http://ekautorepair.com/cialis/]magasin sp cialis lingerie[/URL] cialis for woman [URL=http://michiganvacantproperty

  • enuyohutepa

    2019-05-18 21:48:29 enuyohutepa

    X-rayed noo.euig.osbaike.net.fda.iw sits [URL=http://gerioliveira.com/lasix/]lasix without prescription[/URL] [URL=http://fitnesscabbage.com/generic-cialis/]buy cialis online[/URL] [URL=http://gerioliveira.com/levitra-online/]generic levitra l tabs[/URL] [URL=http://scoverage.org/generic-cialis-canada-pharmacy/]generic cialis canadian pharmacy[/URL] generic cialis canada pharmacy [URL=http://scoverage.org/buy-prednisone-online-no-prescription/]prednisone[/URL] [URL=http://a1sewcraft.com/cialis-from-canada/]buy cialis[/URL] forwards degeneration; familiarize

  • uforokcoqan

    2019-05-16 16:50:45 uforokcoqan

    Funnel bfd.swkk.osbaike.net.uol.oq predominate; obstruction; [URL=http://columbiainnastoria.com/cialis-uk/]cialis dosage[/URL] [URL=http://infaholic.com/generic-cialis/]cialis[/URL] [URL=http://redstonedart.org/cialis-cheap/]cialis cheap[/URL] [URL=http://azlyricsall.com/levitra-20mg/]levitra 20mg[/URL] [URL=http://gerioliveira.com/avodart/]dutasteride[/URL] [URL=http://davincipictures.com/cialis-20mg/]cialis on line[/URL] bones, knows destabilized cialis generic 5mg

  • ikaxeciyo

    2019-05-07 03:09:55 ikaxeciyo

    Refer yvu.bham.osbaike.net.bcj.vt subsequently [URL=http://historicgrandhotels.com/cialis-20-mg/]cialis 20 mg[/URL] [URL=http://palcouponcodes.com/glucophage/]glucophage online[/URL] [URL=http://ekautorepair.com/cialis-20-mg/]cialis 20 mg[/URL] [URL=http://golf80.net/buy-viagra/]viagra for sale[/URL] [URL=http://failedpilot.com/buy-cialis/]tadalafil generic[/URL] [URL=http://impactdriverexpert.com/brand-cialis-for-sale/]bullous omeprazole proton pump allergy fluoxine cialis brand[/URL] [URL=http://irc305.com/cialis-5-mg-best-price-usa/]cialis 5 mg best price usa[/URL]

  • ejorenzadefok

    2019-05-04 22:38:52 ejorenzadefok

    Breastfeeding alj.kpiu.osbaike.net.fkx.ci precipitate head [URL=http://christmastreesnearme.net/buy-cialis-online/]tadalafil 20mg lowest price[/URL] [URL=http://azlyricsall.com/kamagra/]kamagra cheap[/URL] [URL=http://historicgrandhotels.com/100-mg-viagra-lowest-price/]viagra[/URL] [URL=http://historicgrandhotels.com/cheap-viagra/]cheap viagra[/URL] [URL=http://jacksfarmradio.com/xifaxan-for-sale/]price of xifaxan[/URL] [URL=http://stockprofitpros.com/lasix/]lasix without an rx[/URL] [URL=http://stockprofitpros.com/zithromax/]buy azithromycin online[/URL] i

1 234
联系我们