Foreword
Guava 28.0 released, Guava is a Google open source project, contains many of Google's core Java commonly used libraries, such as: collection [collections], the cache [caching], native type support [primitives support], concurrency library [concurrency libraries], Common Annotations [common annotations], string processing [string processing] and I / O and the like.
New features
collect
: Adding Queues based method for someDuration
heavy-duty ( 21d06cf )net
: For application / geo + json addedMediaType
( 36cd5cf )net
: In order toHttpHeaders
add a number of constantsconcurrent
: Removed deprecatedCheckedFuture
and related utilities ( 3dd22fe )concurrent
: Adding a method based on manyDuration
overloadedconcurrent
: FromsetFuture
deleted in@Beta
( 5ec1360 )concurrent
: New deprecatedFluentFuture.from(FluentFuture)
to indicate redundant code ( f9f2807 )graph
: NewGraphBuilder.immutable()
,ValueGraphBuilder.immutable()
andNetworkBuilder.immutable()
, in a smooth manner for constructing immutable pattern- J2ObjC: Fixed using
ImmutableMap.entrySet()
crash caused ( 74fc49f )
Maven relies
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0-jre</version>
<!-- or, for Android: -->
<version>28.0-android</version>
</dependency>
Download and update instructions
https://github.com/google/guava/releases/tag/v28.0
Getting Started
Introduced maven dependence
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0-jre</version>
<!-- or, for Android: -->
<version>28.0-android</version>
</dependency>
Note: The version we can choice
Guava Collection Example
Ordinary collection Collection
List<String> list = Lists.newArrayList();
Set<String> set = Sets.newHashSet();
Map<String, String> map = Maps.newHashMap();
Create an immutable set of Guava
ImmutableList<String> iList = ImmutableList.of("a", "b", "c");
ImmutableSet<String> iSet = ImmutableSet.of("e1", "e2");
ImmutableMap<String, String> iMap = ImmutableMap.of("k1", "v1", "k2", "v2");
immutable objects immutable characteristics
1.在多线程操作下,是线程安全的。
2.所有不可变集合会比可变集合更有效的利用资源。
3.中途不可改变
Map-List Comparison
Longhand
1. Map<String,List<Integer>> map = new HashMap<String,List<Integer>>();
2. List<Integer> list = new ArrayList<Integer>();
3. list.add(1);
4. list.add(2);
5. map.put("test", list);
System.out.println(map.get("test"));
#需要5步,执行结果[1, 2]
Guava wording
1. Multimap<String,Integer> mapM = ArrayListMultimap.create();
2. mapM.put("test",1);
3. mapM.put("test",2);
System.out.println(mapM.get("test"));
#需要3步,执行结果[1, 2]
Note: the results are the same, but the code is nearly half less Oh ~ ~ ~
Guava string connector Joiner
Connecting the plurality of strings and to append StringBuilder
StringBuilder stringBuilder = new StringBuilder("嗨,");
// 字符串连接器,以|为分隔符,同时去掉null元素
Joiner joiner1 = Joiner.on("|").skipNulls();
// 构成一个字符串jim|jack|kevin并添加到stringBuilder
stringBuilder = joiner1.appendTo(stringBuilder, "jim", "jack", null, "kevin");
System.out.println(stringBuilder);
The results: Hey, jim | jack | kevin
The Map into a string
Map<String, String> testMap = Maps.newLinkedHashMap();
testMap.put("Cookies", "12332");
testMap.put("Content-Length", "30000");
testMap.put("Date", "2018.07.04");
testMap.put("Mime", "text/html");
// 用:分割键值对,并用#分割每个元素,返回字符串
String returnedString = Joiner.on("#").withKeyValueSeparator(":").join(testMap);
System.out.println(returnedString);
The results: Cookies: 12332 # Content-Length: 30000 # Date: 2018.07.04 # Mime: text / html
Convert a string Map
// 接上一个,内部类的引用,得到分割器,将字符串解析为map
Splitter.MapSplitter ms = Splitter.on("#").withKeyValueSeparator(':');
Map<String, String> ret = ms.split(returnedString);
for (String it2 : ret.keySet()) {
System.out.println(it2 + " -> " + ret.get(it2));
}
The results:
Cookies -> 12332
Content-the Length -> 30000
a Date -> 2018.07.04
Mime -> text / HTML
String Tools Strings
System.out.println(Strings.isNullOrEmpty("")); // true
System.out.println(Strings.isNullOrEmpty(null)); // true
System.out.println(Strings.isNullOrEmpty("hello")); // false
// 将null转化为""
System.out.println(Strings.nullToEmpty(null)); // ""
// 从尾部不断补充T只到总共8个字符,如果源字符串已经达到或操作,则原样返回。类似的有padStart
System.out.println(Strings.padEnd("hello", 8, 'T')); // helloTTT
Character matcher CharMatcher
Replace blank
// 空白回车换行对应换成一个#,一对一换
String stringWithLinebreaks = "hello world\r\r\ryou are here\n\ntake it\t\t\teasy";
String s6 = CharMatcher.BREAKING_WHITESPACE.replaceFrom(stringWithLinebreaks,'#');
System.out.println(s6);
The results: hello # world ### you # are # here ## take # it ### easy
Continuously shrunk to a blank character
// 将所有连在一起的空白回车换行字符换成一个#,倒塌
String tabString = " hello \n\t\tworld you\r\nare here ";
String tabRet = CharMatcher.WHITESPACE.collapseFrom(tabString, '#');
System.out.println(tabRet);
The results: # hello # world # you # are # here #
Before and after removing the blank and curling up in a character
// 在前面的基础上去掉字符串的前后空白,并将空白换成一个#
String trimRet = CharMatcher.WHITESPACE.trimAndCollapseFrom(tabString, '#');
System.out.println(trimRet);
The results: hello # world # you # are # here
Digital reserved
String letterAndNumber = "1234abcdABCD56789";
// 保留数字
String number = CharMatcher.JAVA_DIGIT.retainFrom(letterAndNumber);
System.out.println(number);
The results: 123456789
Remark
Well, we introduced here, and we often use tools Guava can meet, make code more concise and efficient.