Joiner class Guava Java

JavaServer Side ProgrammingProgramming

Joiner provides various methods to handle joining operations on string, objects, etc. Let us see an example −

Example

import com.google.common.base.Joiner;
import java.util.*;
public class Demo{
   public static void main(String[] args){
      String[] my_arr = { "hel", null, "lo", "wo", "r", null, "ld" };
      System.out.println("The original array is : "+ Arrays.toString(my_arr));
      String my_result = Joiner.on('+').skipNulls().join(my_arr);
      System.out.println("The joined string is : " + my_result);
   }
}

Output

The original array is [hel, null, lo, wo, r, null, ld]
The joined string is hel+lo+wo+r+ld

A class named Demo contains the main function, which defines a string array. The array is converted to string and displayed on the string. The array also contains some null values. While displayed this array, the nulls are removed and replaced with the ‘+’ operator, all because of the Joiner class present in the Guava package. This output is displayed on the console.

raja
Updated on 14-Jul-2020 06:58:05

Advertisements