Guava – Joiner class

  • Post author:
  • Post category:Guava
  • Post comments:0 Comments
Joiner

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

Class Methods

Sr.NoMethod & 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.
5StringBuilder appendTo(StringBuilder builder, Iterable<?> parts)Appends the string representation of each of the parts, using the previously configured separator between each, to builder.
6StringBuilder appendTo(StringBuilder builder, Iterator<?> parts)Appends the string representation of each of the parts, using the previously configured separator between each, to builder.
7StringBuilder appendTo(StringBuilder builder, Object[] parts)Appends the string representation of each of the parts, using the previously configured separator between each, to builder.
8StringBuilder appendTo(StringBuilder builder, Object first, Object second, Object… rest)Appends to builder the string representation of each of the remaining arguments.
9String join(Iterable<?> parts)Returns a string containing the string representation of the each of parts, using the previously configured separator between each.
10String join(Iterator<?> parts)Returns a string containing the string representation of the each of parts, using the previously configured separator between each.
11String join(Object[] parts)Returns a string containing the string representation of each of the parts, using the previously configured separator between each.
12String join(Object first, Object second, Object… rest)Returns a string containing the string representation of each argument, using the previously configured separator between each.
13static Joiner on(char separator)Returns a joiner which automatically places separator between consecutive elements.
14static Joiner on(String separator)Returns a joiner which automatically places separator between consecutive elements.
15Joiner skipNulls()Returns a joiner with the same behavior as this joiner, except automatically skipping over any provided null elements.
16Joiner useForNull(String nullText)Returns a joiner with the same behavior as this one, except automatically substituting nullText for any provided null elements.
17Joiner.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 of Joiner Class

Create the following java program using any editor of your choice in say C:/> Guava

GuavaTester.java

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)));
   }
}

Verify the Result

Compile the class using javac compiler as follows −

C:\Guava>javac GuavaTester.java

Now run the GuavaTester to see the result.

C:\Guava>java GuavaTester

See the result.

1,2,3,4,5,6

Previous Page:-Click Here

Leave a Reply