Java Guava | Longs.concat() method with Examples

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

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

Syntax:

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

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

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

Below programs illustrate the use of concat() method:

Example 1:




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


Output:

Long Array 1: [1, 2, 3, 4, 5]
Long Array 2: [6, 2, 7, 0, 8]
Combined Array: [1, 2, 3, 4, 5, 6, 2, 7, 0, 8]

Example 2:




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


Output:

Long Array 1: [1, 2, 3]
Long Array 2: [4, 5]
Long Array 3: [6, 7, 8]
Long Array 4: [9, 0]
Combined Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

Reference: https://google.github.io/guava/releases/21.0/api/docs/com/google/common/primitives/Longs.html#concat-long:A…-

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button