Guava - Joiner Class



Joiner provides various methods to handle joining operations on string, objects, etc.

Class Declaration

Following is the declaration for com.google.common.base.Joiner class −

@GwtCompatible
public class Joiner
   extends Object
Sr.No Method & Description
1

<A extends Appendable> A appendTo(A appendable, Iterable<?> parts)

Appends the string representation of each of the parts, using the previously configured separator between each, to appendable.

2

<A extends Appendable> A appendTo(A appendable, Iterator<?> parts)

Appends the string representation of each of the parts, using the previously configured separator between each, to appendable.

3

<A extends Appendable> A appendTo(A appendable, Object[] parts)

Appends the string representation of each of the parts, using the previously configured separator between each, to appendable.

4

<A extends Appendable> A appendTo(A appendable, Object first, Object second, Object... rest)

Appends to appendable the string representation of each of the remaining arguments.

5

StringBuilder appendTo(StringBuilder builder, Iterable<?> parts)

Appends the string representation of each of the parts, using the previously configured separator between each, to builder.

6

StringBuilder appendTo(StringBuilder builder, Iterator<?> parts)

Appends the string representation of each of the parts, using the previously configured separator between each, to builder.

7

StringBuilder appendTo(StringBuilder builder, Object[] parts)

Appends the string representation of each of the parts, using the previously configured separator between each, to builder.

8

StringBuilder appendTo(StringBuilder builder, Object first, Object second, Object... rest)

Appends to builder the string representation of each of the remaining arguments.

9

String join(Iterable<?> parts)

Returns a string containing the string representation of the each of parts, using the previously configured separator between each.

10

String join(Iterator<?> parts)

Returns a string containing the string representation of the each of parts, using the previously configured separator between each.

11

String join(Object[] parts)

Returns a string containing the string representation of each of the parts, using the previously configured separator between each.

12

String join(Object first, Object second, Object... rest)

Returns a string containing the string representation of each argument, using the previously configured separator between each.

13

static Joiner on(char separator)

Returns a joiner which automatically places separator between consecutive elements.

14

static Joiner on(String separator)

Returns a joiner which automatically places separator between consecutive elements.

15

Joiner skipNulls()

Returns a joiner with the same behavior as this joiner, except automatically skipping over any provided null elements.

16

Joiner useForNull(String nullText)

Returns a joiner with the same behavior as this one, except automatically substituting nullText for any provided null elements.

17

Joiner.MapJoiner withKeyValueSeparator(String keyValueSeparator)

Returns a MapJoiner using the given key-value separator, and the same configuration as this Joiner otherwise.

Methods Inherited

This class inherits methods from the following class −

  • java.lang.Object

Example - Join Values while Skipping Null Values

GuavaTester.java

package com.tutorialspoint;

import java.util.Arrays;
import com.google.common.base.Joiner;

public class GuavaTester {
   public static void main(String args[]) {
      GuavaTester tester = new GuavaTester();
      tester.testJoiner();	
   }

   private void testJoiner() {
      System.out.println(Joiner.on(",")
         .skipNulls()
         .join(Arrays.asList(1,2,3,4,5,null,6)));
   }
}

Output

Run the GuavaTester and verify the output.

1,2,3,4,5,6

Example - Join Values while Replacing Null Values

GuavaTester.java

package com.tutorialspoint;

import java.util.Arrays;
import com.google.common.base.Joiner;

public class GuavaTester {
   public static void main(String args[]) {
      GuavaTester tester = new GuavaTester();
      tester.testJoiner();	
   }

   private void testJoiner() {
      System.out.println(Joiner.on(",")
         .useForNull("NONE")
         .join(Arrays.asList(1,2,3,4,5,null,6)));
   }
}

Output

Run the GuavaTester and verify the output.

1,2,3,4,5,NONE,6

Example - Join Objects

GuavaTester.java

package com.tutorialspoint;

import java.util.Arrays;
import com.google.common.base.Joiner;

public class GuavaTester {
   public static void main(String args[]) {
      GuavaTester tester = new GuavaTester();
      tester.testJoiner();	
   }

   private void testJoiner() {
      System.out.println(Joiner.on(",")
         .join(Arrays.asList(new Student(1, "Mahesh"), new Student(2,"Ramesh"))));
   }
}

class Student {
   private int rollNo;
   private String name;
   
   public Student(int rollNo, String name){
      this.rollNo = rollNo;
	  this.name = name;
   }
   
   public String toString(){
      return "[ Roll No:" + rollNo + ", Name: " + name +"]";    
   }
}

Output

Run the GuavaTester and verify the output.

[ Roll No:1, Name: Mahesh],[ Roll No:2, Name: Ramesh]
Advertisements