Monday, November 18, 2024
Google search engine
HomeLanguagesJavaJava Guava | Chars.concat() method with Examples

Java Guava | Chars.concat() method with Examples

The concat() method of Chars Class in the Guava library is used to concatenate the values of many arrays into a single array. These char arrays to be concatenated are specified as parameters to this method.

For example: concat(new char[] {a, b}, new char[] {}, new char[] {c}
returns the array {a, b, c}.

Syntax:

public static char[] concat(char[]... arrays)

Parameter: This method accepts arrays as parameter which is the char arrays to be concatenated by this method.

Return Value: This method returns a single char array which contains all the specified char array values in its original order.

Below programs illustrate the use of concat() method:

Example 1:




// Java code to show implementation of
// Guava's Chars.concat() method
  
import com.google.common.primitives.Chars;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating 2 Character arrays
        char[] arr1 = { 'H', 'E', 'L', 'L', 'O' };
        char[] arr2 = { 'G', 'E', 'E', 'K', 'S' };
  
        // Using Chars.concat() method to combine
        // elements from both arrays into a single array
        char[] res = Chars.concat(arr1, arr2);
  
        // Displaying the single combined array
        System.out.println(Arrays.toString(res));
    }
}


Output:

[H, E, L, L, O, G, E, E, K, S]

Example 2:




// Java code to show implementation of
// Guava's Chars.concat() method
  
import com.google.common.primitives.Chars;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating 4 Character arrays
        char[] arr1 = { '1', '2', '3' };
        char[] arr2 = { '4', '5' };
        char[] arr3 = { '6', '7', '8' };
        char[] arr4 = { '9', '0' };
  
        // Using Chars.concat() method to combine
        // elements from both arrays into a single array
        char[] res = Chars.concat(arr1, arr2, arr3, arr4);
  
        // Displaying the single combined array
        System.out.println(Arrays.toString(res));
    }
}


Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Reference: https://google.github.io/guava/releases/18.0/api/docs/com/google/common/primitives/Chars.html#concat(char[]…)

RELATED ARTICLES

Most Popular

Recent Comments