Open In App

Guava’s Ints.join() Function in Java

Last Updated : 23 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Ints.join() function provided by Guava library in Java for working with primitive integer arrays. It simplifies the process of converting an array of integers into a single concatenated string, with a specified delimiter separating the elements. For example, join(“-“, 1, 2, 3) returns the string “1-2-3”

Ints.join() is part of the com.google.common.primitives.Ints class in Guava. It provides a concise and readable way to join the elements of an int[] array into a delimited string.

Example:

Java
import com.google.common.primitives.Ints;
import java.util.Arrays;

class IntsJoinExample {

    public static void main(String[] args)
    {

        int[] a = { 2, 4, 6, 8, 10 };

        // Using Ints.join() method to get a
        // string containing the elements of array
        // separated by a separator
        System.out.println(Ints.join(", ", a));
    }
}

Output:

2, 4, 6, 8, 10

Syntax: 

public static String join(String separator, int... array)

Parameters: This method takes the following parameters:  

  • separator: A String that acts as the delimiter between the joined elements. (but not at the start or end).
  • array: A varargs or array of integers (int[]) to be joined.

Return Value: This method returns a string containing the supplied int values separated by separator

Example:

Java
import com.google.common.primitives.Ints;
import java.util.Arrays;

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

        int[] arr = { 3, 5, 7, 9, 11 };

        // Using Ints.join() method to get a
        // string containing the elements of array
        // separated by a separator
        System.out.println(Ints.join("-", arr));
    }
}

Output
3-5-7-9-11




Next Article
Article Tags :
Practice Tags :

Similar Reads