GoogleGuava概述1.Guava是来自Google的一组核心Java库,包括新的集合类型(例如multimap和multiset)、不可变集合、图形库以及用于并发、I/O、散列、缓存的库,原语、字符串等等!广泛应用于谷歌的大部分Java项目,也被许多其他公司广泛使用。2、Guavagithub开源地址:GitHub-google/guava:https://github.com/google/guava3。官网使用手册https://github.com/google/gua...4.com.google.guava依赖项:com.google.guavaguava29.0-jre不可变集合和对象1.使对象不可变副本是一种很好的防御性编程技术,不可变对象有很多优点,包括:不受信任的库可以安全使用。线程安全:可以被多个线程使用而没有竞争风险。不需要支持突变,并且为了节省时间和空间,所有不可变集合实现都比它们的可变集合实现具有更高的内存效率。可以用作常量并期望它保持不变。2.要点:每个Guava不可变集合实现都拒绝空值。Guava的设计建议使用空值。在大多数情况下,当遇到空值时会抛出异常。3、一个不可变的ImmutableXxx集合可以通过以下方式创建:使用copyOf方法,比如ImmutableSet.copyOf(set)的方法,比如ImmutableSet.of("a","b","c")或者ImmutableMap.of("a",1,"b",2)使用Builder方法,4、Guava为javajdk的每个标准集合类型都提供了简单易用的不可变版本,包括Guava自带的集合变体。为java提供的不可变版本都继承了javajdk的接口,所以操作上基本没有区别。Theimplementationclassesofthefollowinginterfacesalsohavecorrespondingimmutableversions.接口JDK或者Guava不可变版本CollectionJDKImmutableCollectionListJDKImmutableListSetJDKImmutableSetSortedSet/NavigableSetJDKImmutableSortedSetMapJDKImmutableMapSortedMapJDKImmutableSortedMapMultisetGuavaImmutableMultisetSortedMultisetGuavaImmutableSortedMultisetMultimapGuavaImmutableMultimapListMultimapGuavaImmutableListMultimapSetMultimapGuavaImmutableSetMultimapBiMapGuavaImmutableBiMapClassToInstanceMapGuavaImmutableClassToInstanceMapTableGuavaImmutableTable在线演示源码:https://github.com/main/java/...官网文档https://github.com/google/gua...推荐一个SpringBoot基础教程和实例:https://github.com/javastacks...Guava新的集合类型1.Guava引入了很多新的集合类型,这些是JavaJDK中没有的,但非常有用。这些是为了与JDK兼容。集合框架旨在愉快地共存,而不是将东西塞进JDK集合抽象中。Multiset可重复集合1.Guava提供了一种新的集合类型Multiset,支持添加多个相同的元素,其中成员可以出现多次。2、Multiset等价于Set。不同的是Multiset可以添加相同的元素。它使用一个HashMap在内部维护它。3、Multiset也有自己的实现类。常用的有HashMultiset、LinkedHashMultiset、TreeMultiset等。HashMultiset和TreeMultiset是无序的,LinkedHashMultiset是有序的,操作和JDK的HashSet、TreeSet、LinkedHashSet完全一样。在线demo源码https://github.com/wangmaoxio...MultimapMultimap1.每个有经验的Java程序员都实现过Map>或者Map>,Guava的Multimap框架使得很容易处理从一个键到多个值的映射。多重映射是将键与任意数量的值相关联的通用方法。2.Conceptually,therearetwowaystothinkofamultimapasacollectionofmappingsfromasinglekeytoasinglevalue:3.Multimapprovidesmultipleimplementations:MultimapimplementskeyusingvalueArrayListMultimapHashMapArrayListHashMultimapHashMapHashSetLinkedListMultimap*LinkedHashMap*LinkedList*LinkedHashMultimap**LinkedHashMapLinkedHashSetTreeMultimapTreeMapTreeSetImmutableListMultimapImmutableMapImmutableListImmutableSetMultimapImmutableMapImmutableSet4.除了不可变的实现,每个实现都支持null键和值。并非所有实现都实现为Map>(特别是一些Multimap实现使用自定义哈希表来最小化开销。)在线demo源码https://github.com/wangmaoxio...推荐一个SpringBoot基础教程和实例:https://github.com/javastacks...BiMap双向映射1.映射值的传统backtokeys的做法是维护两个独立的map并保持同步,但是这样很容易出错,并且当一个值已经存在于map中时MapnameToId=Maps.newHashMap();MapidToName=Maps.newHashMap();nameToId.put("Bob",42);idToName.put(42,"Bob");2、BiMap提供了多种实现:key-value映射实现,value-key映射实现对应BiMapHashMapHashMapHashBiMapImmutableMapImmutableMapImmutableMapImmutableBiMapEnumMapEnumBiMapEnumMapHashMapEnumHashBiMap在线Demo源码:https://github.com/wangmaoxio...Table表结构数据1.尝试建立索引时一次在多个键上,你会得到一个像Map>这样的代码,它很难用而且很难用。Guava提供了一个新的集合类型Table,它支持任何“行”类型和“列”类型的这种用例。2、Table提供多种实现:HashBasedTable:HashMap>基本支持。TreeBasedTable:TreeMap>基本支持。ImmutableTableArrayTable:需要在构造时指定完整的行列范围,但在表密集时支持二维数组以提高速度和内存效率。ArrayTable的工作原理与其他实现有些不同。在线演示源代码:https://github。com/wangmaoxio...ClassToInstanceMap类型映射到实例1。有时候key不是单一类型,而是多种类型。Guava为此提供了ClassToInstanceMap。键可以是多种类型,值是该类型的一个实例。2、ClassToInstanceMap的实现包括:MutableClassToInstanceMap和ImmutableClassToInstanceMap的实现。在线demo源码:https://github.com/wangmaoxio...JDK集合辅助工具类1.任何使用过JDK集合框架的程序员都知道并喜欢Guava提供的实用程序java.util.Collections。许多适用于集合的静态方法实用程序。接口属于JDK还是Guava对应GuavaAPICollectionJDKCollections2ListJDKListsSetJDKSetsSortedSetJDKSetsMapJDKMapsSortedMapJDKMapsQueueJDKQueuesMultisetGuavaMultisetsMultimapGuavaMultimapsBiMapGuavaMapsTableGuavaTablesLists在线演示:https://github.com/wangmaoxio...Sets在线演示:https://github.com/wangmaoxio...JDK基本类型辅助工具类1、Guava为JavaThebasictypesofJDKprovideutilityclasses:BasictypesGuavaauxiliarytoolclassesbyteBytes,SignedBytes,UnsignedBytesshortShortsintInts,UnsignedInteger,UnsignedIntslongLongs,UnsignedLong,UnsignedLongsfloatFloatsdoubleDoublescharsbooleanBooleanntsOnlinedemosourcecode:https://github.com/https://github.com/src/main/j...booleansonlinedemosourcecode:https://github.com/src/main/j...othertypesarethesame.JDKstringauxiliarytoolclass1,Stringsclassprovidesafewcommonlyusedstringutilities.Onlinedemosourcecode:https://github.com/wangmaoxio...2.Joinerisaconnectorforconnectingelementsinjava.lang.Iterable,java.util.Iterator,java.lang.Object[].Onlinedemosourcecode:https://github.com/wangmaoxio...3.Splitterisasplitterforsplittingcharactersequencesjava.lang.CharSequence.在线demo源码:https://github.com/wangmaoxio...4.CharMatcher字符匹配器,用于匹配字符,CharMatcher可以看做是代表一种特定类型的字符,比如数字或者空格。注意:CharMatcher只处理char值。在线demo源码:https://github.com/wangmaoxio...StopwatchStopwatch1、Google的秒表Stopwatch相对于Springframewrk核心包和apachecommonslang3包的秒表,使用起来最方便。2.这个类不是线程安全的。/***StopwatchcreateStarted():创建(并启动)一个新的秒表,使用System#nanoTime作为其时间源。*StopwatchcreateUnstarted():创建(但不启动)一个新的秒表,使用System#nanoTime作为其时间源。*longelapsed(TimeUnitdesiredUnit):返回此秒表上显示的当前经过时间,以所需时间单位表示,所有小数部分向下舍入*booleanisRunning():如果已在此秒表上调用start()},并停止()自上次调用start()以来未被调用,返回true*Stopwatchreset():将此秒表的经过时间设置为零并将其置于停止状态。*Stopwatchstart():启动秒表,如果秒表已经在运行,IllegalStateException*Stopwatchstop():停止秒表,未来的读取将返回到目前为止经过的固定持续时间。*tringtoString():返回当前运行时间的字符串表示,比如2.588s,106.8ms*/@TestpublicvoidtestStopwatch()throwsInterruptedException{SecureRandomsecureRandom=newSecureRandom();秒表stopwatch=Stopwatch.createStarted();intnextInt=secureRandom.nextInt(2000);System.out.println("任务1预算时间消耗:"+nextInt);//任务1预算时间消耗:81TimeUnit.MILLISECONDS.sleep(nextInt);系统输出。println("\t任务1实际耗时:"+stopwatch.elapsed(TimeUnit.MILLISECONDS)+"(毫秒)");//任务1实际耗时:563(毫秒)stopwatch.reset().start();nextInt=secureRandom.nextInt(4000);System.out.println("任务2预算时间消耗:"+nextInt);//任务2预算时间消耗:1591TimeUnit.MILLISECONDS.sleep(nextInt);系统输出。println("\t任务2实际耗时:"+stopwatch.toString());//任务2实际耗时:1.592sstopwatch.reset().start();nextInt=secureRandom.nextInt(3000);System.out.println("任务3预估耗时:"+nextInt);//任务3预估耗时:1964TimeUnit.MILLISECONDS.sleep(nextInt);西stem.out.println("\tTask3实际耗时:"+stopwatch.stop().toString());//Task3实际耗时:1.965s}感觉不错,就用吧!版权声明:本文为CSDN博主“蚩尤后裔”原创文章,遵循CC4.0BY-SA版权协议,转载请附上原文出处链接及本声明