Google Guava组件

Google Guava组件

介绍

据说Google内部一开始是使用Apache Commons 下的工具包来开发的,但是后来随着开发觉得其无法很好的满足公司内部开发,因此就自己造轮子了。

相比Apache Commons Collections等工具包,Guava确实更现代和更符合java约定。

备注:如果是新项目个人推荐Guava,当然不适用这些组件完全可以。因为它们都是基于JDK的扩展。

核心模块

collect模块

BiMap(双向映射Map结构)

    实现类:
        HashBiMap、ImmutableBiMap、EnumBiMap、EnumHashBiMap

当有这种场景时,key需要两个值来确定比如横坐标和纵坐标确定城市位
Map<int x,int y,String string>
但是可惜的是Java并没有这样的数据结构,我们就可以以将x和y抽象到一个对象中,比如Position类,但是这样显然就复杂了我们的开发过程。
Guava提供了Table<R,C,V>数据结构。
我以上描述的并完全正确,Table的操作原理和以上是略有不同的。


Table

    Typically, when you are trying to index on more than one key at a time,  
    you will wind up with something like Map<FirstName, Map<LastName, Person>>, which is ugly and awkward to use.   
    Guava provides a new collection type, Table, which supports this use case for any "row" type and "column" type. 
    Table supports a number of views to let you use the data from any angle, including

    备注:理解其数据结构在特定场景下使用能获得不错的效益

    实现:
        HashBasedTable、TreeBasedTable、ImmutableTable、ArrayTable



实际开发中我们可能会遇到,计算某个元素在集合中出现的次数,我们通常的做法就是迭代遍历一下,自己计算。Guava就提供了这么一个数据结构给我们使用Multiset。
同Apache commons Collection的Bag数据结构类似。

Multiset

    A collection that supports order-independent equality, like Set, but may have duplicate elements.
    A multiset is also sometimes called a bag

    实现类:HashMultiset、TreeMultiset、LinkedHashMultiset、ConcurrentHashMultiset、ImmutableMultiset


Multimap

    Every experienced Java programmer has, at one point or another, implemented a Map<K, List<V>> or Map<K, Set<V>>, and dealt with the awkwardness of that structure. For example, Map<K, Set<V>> is a typical way to represent an unlabeled directed graph. Guava's Multimap framework makes it easy to handle a mapping from keys to multiple values. A Multimap is a general way to associate keys with arbitrarily many values.


    实现:ArrayListMultimap、HashMultimap、TreeMultimap等


范围集合在开发中也会遇到
RangeSet

    A RangeSet describes a set of disconnected, nonempty ranges. When adding a range to a mutable RangeSet, any connected ranges are merged together, and empty ranges are ignored. 

总结

Guava组件内容很多,推荐看自己感兴趣的模块。说实话各种数据结构很丰富,但是让我有一种乱花渐欲迷人眼的感觉,看看就行。

我个人更加推荐好好掌握好JDK基础的集合数据结构,Guava都是基于JDK的扩展。

参考

1、https://github.com/google/guava/wiki

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值