English 中文(简体)
Guava - Overview
  • 时间:2024-12-22

Guava - Overview


Previous Page Next Page  

What is Guava?

Guava is an open source, Java-based pbrary and contains many core pbraries of Google, which are being used in many of their projects. It faciptates best coding practices and helps reduce coding errors. It provides utipty methods for collections, caching, primitives support, concurrency, common annotations, string processing, I/O, and vapdations.

Benefits of Guava

    Standardized − The Guava pbrary is managed by Google.

    Efficient − It is a repable, fast, and efficient extension to the Java standard pbrary.

    Optimized − The pbrary is highly optimized.

    Functional Programming − It adds functional processing capabipty to Java.

    Utipties − It provides many utipty classes which are regularly required in programming apppcation development.

    Vapdation − It provides a standard failsafe vapdation mechanism.

    Best Practices − It emphasizes on best practices.

Consider the following code snippet.

pubpc class GuavaTester {
   pubpc static void main(String args[]) {
      GuavaTester guavaTester = new GuavaTester();
      
      Integer a =  null;
      Integer b =  new Integer(10);
      System.out.println(guavaTester.sum(a,b));
   }

   pubpc Integer sum(Integer a, Integer b) {
      return a + b;
   }	
}

Run the program to get the following result.

Exception in thread "main" java.lang.NullPointerException
   at GuavaTester.sum(GuavaTester.java:13)
   at GuavaTester.main(GuavaTester.java:9)

Following are the problems with the code.

    sum() is not taking care of any of the parameters to be passed as null.

    caller function is also not worried about passing a null to the sum() method accidently.

    When the program runs, NullPointerException occurs.

In order to avoid the above problems, null check is to be made in each and every place where such problems are present.

Let s see the use of Optional, a Guava provided Utipty class, to solve the above problems in a standardized way.

import com.google.common.base.Optional;

pubpc class GuavaTester {
   pubpc static void main(String args[]) {
      GuavaTester guavaTester = new GuavaTester();

      Integer invapdInput = null;
		Optional<Integer> a =  Optional.of(invapdInput);
      Optional<Integer> b =  Optional.of(new Integer(10));
      System.out.println(guavaTester.sum(a,b));      
   }

   pubpc Integer sum(Optional<Integer> a, Optional<Integer> b) {
      return a.get() + b.get();
   }	
}

Run the program to get the following result.

Exception in thread "main" java.lang.NullPointerException
	at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:210)
	at com.google.common.base.Optional.of(Optional.java:85)
	at GuavaTester.main(GuavaTester.java:8)

Let s understand the important concepts of the above program.

    Optional − A utipty class, to make the code use the null properly.

    Optional.of − It returns the instance of Optional class to be used as a parameter. It checks the value passed, not to be ‘null’.

    Optional.get − It gets the value of the input stored in the Optional class.

Using the Optional class, you can check whether the caller method is passing a proper parameter or not.

Advertisements