Guava is an open source library released by Google. It mainly provides some very useful tool classes and APIs in Java development, such as string processing, set operations, functional programming, caching, etc. wait.
Strings is a set of string tools provided by Guava, which provides many useful methods to process strings. The following are the main methods of Strings:
isNullOrEmpty(String string)
: Determine whether the string is empty or null.
padEnd(String string, int minLength, char padChar)
: Pad the specified characters at the end of the string until the string reaches the specified length.
padStart(String string, int minLength, char padChar)
: Pad the specified characters at the beginning of the string until the string reaches the specified length.
repeat(String string, int count)
: Repeat the specified string a specified number of times.
commonPrefix(CharSequence a, CharSequence b)
: Get the longest common prefix of two strings.
commonSuffix(CharSequence a, CharSequence b)
: Get the longest common suffix of two strings.
The following are examples of using Strings:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
Guava provides some very useful collection operation APIs, as follows Display:
1.ImmutableList
Immutable collection is an important feature of Guava, which can ensure that the collection is not modified, thereby avoiding the problem of concurrent access. ImmutabelList is an implementation of immutable List. Here is a sample code:
1 2 |
|
2.Iterables
The Iterables class provides some useful methods to operate collections, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
3.Multimaps
Multimaps provides a very useful data structure that allows one key to correspond to multiple values. Here is a sample code:
1 2 3 4 5 |
|
4.Maps
Maps provides some useful methods to operate Map, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Preconditions is a set of precondition checking tools provided by Guava, which Some methods are provided to check whether parameters are as expected. The following are the main methods of Preconditions:
checkArgument(boolean expression, String errorMessageTemplate, Object... errorMessageArgs)
: Check whether the parameters meet expectations and throw IllegalArgumentException Exceptions can contain error message templates and placeholders.
checkNotNull(T reference, String errorMessageTemplate, Object... errorMessageArgs)
: Check whether the parameter is null and throw a NullPointerException exception, which can contain an error message template and placeholders.
checkState(boolean expression, String errorMessageTemplate, Object... errorMessageArgs)
: Check whether the object state meets expectations and throw an IllegalStateException exception, which can contain error information Templates and placeholders.
checkElementIndex(int index, int size, String errorMessageTemplate, Object... errorMessageArgs)
: Check whether the subscript is within the range of the collection and throw IndexOutOfBoundsException Exceptions can contain error message templates and placeholders.
checkPositionIndex(int index, int size, String errorMessageTemplate, Object... errorMessageArgs)
: Check whether the subscript is within the range of the set and can be equal to the set size, and throws an IndexOutOfBoundsException exception, which can contain error message templates and placeholders.
checkPositionIndexes(int start, int end, int size)
: Check whether the start subscript and end subscript are within the range of the collection, and throw an IndexOutOfBoundsException exception .
The following is an example of using Preconditions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Cache is a caching tool provided by Guava Class, which can help us cache data in memory and improve program performance. The following are the main methods of Cache:
get(K key, Callable valueLoader): Get the cache value of the specified key. If it is not in the cache, call valueLoader to load the data. and stored in cache.
getIfPresent(Object key): Get the cache value of the specified key. If it is not in the cache, return null.
getAllPresent(Iterable keys): Get the cache value of the specified keys. If there is no cache value, return null.
put(K key, V value): Store the cache value of the specified key in the cache.
putAll(Map m): Store the cache value of the specified Map into the cache.
invalidate(Object key): Delete the cache value of the specified key from the cache.
invalidateAll(Iterable keys): Delete the cache value of the specified keys from the cache.
invalidateAll(): Delete all cached values from the cache.
size(): Get the number of cached values in the cache.
asMap(): Convert cache to Map.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The above is the detailed content of What are the tips for using Java Guava?. For more information, please follow other related articles on the PHP Chinese website!