Java Guava | Bytes.concat() method with Examples

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

For example: concat(new byte[] {1, 2}, new byte[] {3, 4, 5}, new byte[] {6, 7}
returns the array {1, 2, 3, 4, 5, 6, 7}.

Syntax:



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

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

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

Below programs illustrate the use of concat() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

Reference: https://google.github.io/guava/releases/19.0/api/docs/com/google/common/primitives/Bytes.html#concat(byte[]…)



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.