Java programmers recommend using Google Guava programming

  • DK provided String is not enough, okay?

  • Joiner/Splitter

  • CharMatcher

  • Ints

  • Multiset

  • Multiset Code

  • unmodifiable

  • defensive copies

  • Immutable

  • ImmutableMap

  • Multimap

  • Bhinap

  • Table

  • Functions

  • Assertion: Predicate

  • filter

  • Cache is king

  • CacheLoader

  • Asynchronous callbacks

  • Summary

  •  
    JDK provided String is not enough, okay?
    Perhaps not friendly, at least let us use them not Goushuang, you have to worry about! For chestnuts, such as the split method of String offer, we have to care about the empty string it, there are elements of it will have to consider returning null result, only the front and rear trim provides a method of (if I want to trim elements in the middle of it).
    Well, look at the following code example, guava let you do not have to worry about these:


    Joiner / Splitter
    Joiner is a connector, a divider Splitter, typically we will define them as static final, after use on the application to generate a String object is processed, it can be multiplexed. To know apache commons StringUtils provided are static method. More importantly, guava provides Joiner / Splitter is a well-tested, its stability and efficiency much higher than the apache, the next you can find yourself a test - we did not want to do what String is to generate your own customized Joiner / Splitter, how straightforward, simple, smooth API! For Joiner, commonly used method is to skip NULL element: skipNulls () / NULL element for use alternatives: useForNull (String) for Splitter, commonly used methods are: trimResults () / omitEmptyStrings () . Note split mode, a character string, as well as regular, there are a fixed length divided (so sweet!)
    In fact, in addition to Joiner / Splitter, guava string matching is further provided: CharMatcher
    CharMatcher
    CharMatcher, character matching and approach to decoupling, and provide rich for you to use!

  • Ints

    guava provides Bytes / Shorts / Ints / Iongs / Floats / Doubles / Chars / Booleans these basic data types of extended support, only you think, no it is not!

  •  

     

    unmodifiable

    You see unmodifiable defect JDK provides yet? In fact, the collection and source collection Collections.unmodifiableXxx returned by the same object, but the API can make changes to the collection were override, will throw UnsupportedOperationException. That we change the source collection, resulting in an immutable view (unmodifiable View) will change, oh my god!

    Of course, without the use of guava, and how we are to avoid the above problems?

  • Multimap
    Can-many: Multimap
    the JDK available to us Map is a key and a value, one to one, then the actual development, there is apparently a case of multiple KEY VALUE (such as a book under the classification), we often express this: Map <k, List>, seems a bit bloated! Bloated no problem, even more unhappy things, we have to determine whether there is KEY to decide whether a new LIST out a bit of trouble! More troublesome things yet to come, such as traversal, such as deleting, SO Hard ......
    Multimap
    under Tips, a collection of guava has all the create method, so the advantage is simple, and we do not have to repeat information generics a. get () / keys () / keySet () / values () / entries () / asMap () returns the view collection methods are very useful. Multimap implementation class has: ArrayListMultimap / HashMultimap / LinkedHashMultimap / TreeMultimap / ImmutableMultimap / ......

  • Multiset

    Multiset是什么,我想上面的图,你应该了解它的概念了。Multiset就是无序的,但是可以重复的集合,它就是游离在List/Set之间的“灰色地带”!(至于有序的,不允许重复的集合嘛,guava还没有提供,当然在未来应该会提供UniqueList,我猜的,哈哈)

    来看一个Multiset的示例:

  •  

    可不可以双向:BiMap
    JDK提供的MAP让我们可以find value by key,那么能不能通过find key by value呢,能不能KEY和VALUE都是唯一的呢。这是一个双向的概念,即forward+backward。在实际场景中有这样的需求吗?比如通过用户ID找到mail,也需要通过mail找回用户名。没有guava的时候,我们需要create forward map AND create backward map,and now just let guava do that for you.

     

    BiMap
    biMap / biMap.inverse() / biMap.inverse().inverse() 它们是什么关系呢?你可以稍微看一下BiMap的源码实现,实际上,当你创建BiMap的时候,在内部维护了2个map,一个forward map,一个backward map,并且设置了它们之间的关系。因此,biMap.inverse() != biMap ;biMap.inverse().inverse() == biMap


  • 可不可以多个KEY:Table
    我们知道数据库除了主键外,还提供了复合索引,而且实际中这样的多级关系查找也是比较多的,当然我们可以利用嵌套的Map来实现:Map<k1,Map<k2,v2>>。为了让我们的代码看起来不那么丑陋,guava为我们提供了Table。


  • Cache is king
    对于大多数互联网项目而言,缓存的重要性,不言而喻!如果我们的应用系统,并不想使用一些第三方缓存组件(如redis),我们仅仅想在本地有一个功能足够强大的缓存,很可惜JDK提供的那些SET/MAP还不行!


    CacheLoader
    首先,这是一个本地缓存,guava提供的cache是一个简洁、高效,易于维护的。为什么这么说呢?因为并没有一个单独的线程用于刷新 OR 清理cache,对于cache的操作,都是通过访问/读写带来的,也就是说在读写中完成缓存的刷新操作!其次,我们看到了,我们非常通俗的告诉cache,我们的缓存策略是什么,SO EASY!在如此简单的背后,是guava帮助我们做了很多事情,比如线程安全。
    让异步回调更加简单

  •  

    JDK中提供了Future/FutureTask/Callable来对异步回调进行支持,但是还是看上去挺复杂的,能不能更加简单呢?比如注册一个监听回调。

     

    异步回调
    我们可以通过guava对JDK提供的线程池进行装饰,让其具有异步回调监听功能,然后在设置监听器即可!

     

     

     

    Functions
    上面的代码是为了完成将List集合中的元素,先截取5个长度,然后转成大写。函数式编程的好处在于在集合遍历操作中提供自定义Function的操作,比如transform转换。我们再也不需要一遍遍的遍历集合,显著的简化了代码!

  •  

    filter
    需要注意的是Lists并没有提供filter方法,不过你可以使用Collections2.filter完成!
    check null and other:Optional、Preconditions
    在guava中,对于null的处理手段是快速失败,你可以看看guava的源码,很多方法的第一行就是:Preconditions.checkNotNull(elements);
    要知道null是模糊的概念,是成功呢,还是失败呢,还是别的什么含义呢?
     

     

发布了22 篇原创文章 · 获赞 10 · 访问量 3万+

Guess you like

Origin blog.csdn.net/hnhygkx/article/details/104689665