Common Guava Method

Source: Internet
Author: User

Common Guava Method
Introduction

  Guava is a java-based Open-Source Tool library of Google. It contains many core libraries widely relied on by Google's java projects,

For example: Set, cache, native type support, concurrent library, General annotation, string processing, I/O, and so on. All these tools are applied to product services by Google engineers every day.

Why do we need to choose Guava during project development:
• Guava is an ice cream (essence) on a java API cake)
• Efficient and well-designed APIs
• Designed, implemented, and used by google developers
• Follow the good syntax practices of the efficient java book
• Improved code scale, simplicity, and simplicity
• Use java 1.5 features
• Popular APIs and dynamic development
• It provides a large number of related application classes, collections, multithreading, comparison, String, input and output, cache, network, native type, mathematics, reflection, and so on.
• Hundreds of thousands of unit tests are used by many projects to help developers focus on business logic rather than writing java application classes
• Saves time, resources, and increases productivity
• My goal is to provide open-source code support for basic java features, rather than writing another
• ApacheCommon library Apache is a mature library, but does not support generics. Apache is useful for early java versions-earlier than 1.5
• The latest java in java 7 and java 8 support some guava APIs

The flexible use of Guava in appropriate scenarios in project development can save us complicated data processing, and the code introduction is easy to use and error-free,

Enhanced code readability and robustness gives us more time to focus on business processing. In short, it makes our code more elegant.

Maven dependency

 

1 <dependency>2     <groupId>com.google.guava</groupId>3     <artifactId>guava</artifactId>4     <version>19.0</version>5 </dependency>
String checking tool
1/** 2 * use of the Guava Strings tool class, null and empty judgment and conversion 3 * @ author chenleixing 4 */5 public void testStrings () {6 Strings. isNullOrEmpty (""); // returns true 7 Strings. nullToEmpty (null); // "8 Strings. nullToEmpty ("chen"); // return "chen" 9 Strings. emptyToNull (""); // return null 10 Strings. emptyToNull ("chen"); // returns "chen" 11 12 Strings. commonPrefix ("aaab", "aac"); // "aa" Otherwise, "" 13 Strings. commonSuffix ("aaac", "aac"); // "aac" Otherwise, "" 14} is returned}
NULL Value Check prerequisite check
1 // judge the Boolean value of parameter 1. If the exception information of the second parameter is thrown as false, IllegalArgumentException2 Preconditions. checkArgument (I> 0, "Argument was % s but expected nonnegative ");
Determines whether the object is null.
1 // judge the Boolean value of parameter 1. If it is null, The NullPointerException2 Preconditions. checkNotNull (null, "This object is Null") is thrown ");
Operation set

Before

1     Map<String, Map<Long, List<String>>> map = new HashMap<String, Map<Long,List<String>>>();  

Use Guava

1 Map<String, Map<Long, List<String>>> map = Maps.newHashMap();

Before

1 List<String> list = new ArrayList<String>();2 list.add("a");3 list.add("b");4 list.add("c");5 list.add("d");

Now

1 List<String> list =Lists.newArrayList("a","b","c","d");

Refer:

Guava official tutorial (Chinese version) http://ifeve.com/google-guava/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.