JAVA

  minte9
REMEMBERS




Last update:   30-03-2022

Guava

Guava is a set of core Java libraries from Google.
 
/**
 * Guava - Null optional example
 */

package com.minte9.optional;
import com.google.common.base.Optional;

public class GuavaNull {
    public static void main(String[] args) {

      // Optional

      Optional<Integer> possible = Optional.of(5);
      System.out.println(possible.isPresent()); // true
      System.out.println(possible.get()); // 5

      // Without Guava

      try {
        String a = null;
        System.out.println(a.length());
      } catch (NullPointerException e) {}

      // Guava Optional

      String b = null;
      Optional<String> c = Optional.fromNullable(b);
      if (c.isPresent()) {
        System.out.println(c.get());
      }
  }
  
}

Join

The fluent style of Joiner makes joining very simple.
 
/**
 * Guava - Joiner example
 */

package com.minte9.joiner;
import com.google.common.base.Joiner;

public class GuavaJoiner {
    
    public static void main(String[] args) {
        
        Joiner joiner = Joiner.on("; ").skipNulls();
        System.out.println(
            joiner.join("a", "b", null, 1, 2) // a; b; 1; 2
        );
    }
}

Pom

Guava's Maven group ID is com.google.guava, and its artifact ID is guava.
 
<?xml version="1.0" encoding="UTF-8"?>
<project 
    xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
        https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.minte9</groupId>
    <artifactId>demoGuavaNull</artifactId>
    <packaging>jar</packaging>
    <version>0.1.0</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.1-jre</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Questions     github Maven
Quick start
        A B C D E F
🔔
1/2