Guava Collection
Zero to Hero
@
I have a dream today!
Guava
Google Java
•
• JCF Java Collection Framework
Google Collections Library
• JDK Functional/caching/
range/hashing
Guava
Google Collections Library
Apache Common Collections
Guava
2002
2.0
2002
1.0
2003
3.0
2004
3.1
2006
3.2
2008
3.2.1
2007.10.22 2008.8 2009.6
2010.4
RC
2011.9
10.0
2011.12
11.0
2012
12&13&14
2013
15.0
2014
16&17&18
2015
19.0
2013.12
4.0
Java5 Java6
Guava
3/ Apache Collection JDK
4/ Java5
2/ Apache Collection Library warning
1/
It does not use generics, which is a problem for
us as we hate to get compilation warnings from
our code.
Guava
Bug Reports
&
Feature Requests
are our
Life Blood.
Bug Reports
&
Feature Requests
are our
Life Blood.
Guava
cgdecker@gmail.com
wasserman.louis@gmail.com
kevinb@google.com
Effective JavaJosh Bloch
Guava
Some
Guava
1/
2/
Guava
Collection
Vistor
fields
1/ null reference
2/ static fields
3/ array array
java.lang.instrument.getObjectSize
The result may include some or all of
the object's overhead
Vistor
call
DFS
Optional
"I call it my billion-dollar mistake." - Sir C. A. R. Hoare
Optional API API
JVM
It should be considered that the approach has been
developed in terms of writing a new application, rather
than maintaining an existing one.
—Stephen Colebourne
Optional
• Optional Field
• Optional API
• Optional getter
• setter Optional
Domain null null
Maybe
Preconditions & Predicates
~
checkState
checkArgument if
null
toString
Come on~ toString
Immutable
: Immutable
1/ defensive programming
2/ thread-safe
3/
sorted copy, Google
Performance note [immutableSortedCopy]:
According to our benchmarking on Open JDK 7, this method is the
most efficient way to make a sorted copy of a collection.
no
Immutable
ImmutableMap
Google ImmutableMap HashMap
1/ 2/ resize
ImmutableMap
Key: Integer Key: String
HashMap HashMap
ImmutableMap equals hashCode
ImmutableMap
ImmutableMap entry hash
equals ImmutbleMap equals Integer
String Integer equals String
hashCode equals
ImmutableMap
Integer equals
instance
4s instance
ImmutableMap
String hashCode
equals
String
String
hashhash
Urls
String
eg. log
Multi
MultiSet
Set
List add size
MultiSet Map<E, Integer>
Integer
Apache Common Library Bag MultiSet Map
MultiSet
MultiSet dinstinct
MultiMap
key Value
k1->v1; k1->v2; k2->v3
Map k1->[v1, v2], k2->[v3]
MultiMaps
MultiMap
tip#1 ImmutableListMultiMap
Immutable
ListMultiMap UnSupportedOperation remove, clear, put, putAll, replaceValues.
element
MultiMaps
tip#2
index entry multiMap Function
Key
id multiMap
tip#3 Predicate
view
thread-safe serializable
Lists Iterable
Lists
/partition
/reverse
view Collections.reverse()
/charactersOf
transform
transform
view
1. view
2. transform add addAll, set
3. Lazy
filter size(), contains()
trap: new ArrayList(Collection) toArray size()
transform
4.
transform
transform
Iterables & FluentIterable
At Google we work with very large quantities of data, which may
be too large to fit in memory, but which can be traversed from
beginning to end in the course of some computation.
remove concat cycle
Iterables lazy
java8 Stream
Other Collections
EvictingQueue not thread-safe non-blocking
Wrapper
Happy Hashing
seed HashFunction RPC Function
Stuff
Enum&String
Ordering
Comparator Reverse
JodaTime
Java Date 90s
Formatter ( + Runtime )
It’s mine.
JodaTime
Say ByeBye to Date Mybatis
RangeSet
10:30 11:3011:00 11:45
1 2 3 4
BloomFilter
By Burton Howard Bloom
m:slot
n:
k: hash
BloomFilter
(1%)
40000000/8/1024/1024=4.7MB
#Murmur(92 ns) #FNV-1a(86 ns) #FNV-1 (92 ns) #DBJ2a (91 ns) #DJB2 (93ns) = 450ns
O(1)
305MB for HashSet(without resize)
Easier	
Cheaper	
Happier
Easier	
Cheaper	
Happier
Reference
• Guava
• Stackoverflow
• Wikipedia
• Oracle
• http://goo.gl/bSVZ1S
• http://goo.gl/YsbSt8
• https://goo.gl/iCL6u8
• http://goo.gl/wNW5Yv
•

Guava collection-zero-to-hero