Guava - Joiner class


Advertisements

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.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 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
guava_string_utilities.htm
Advertisements