Here's what I came up with. There are several ways to do this and they depend on the tools you are using.


Using StringUtils and ArrayUtils from Commons Lang:

Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = StringUtils.join(ArrayUtils.toObject(arr), " - ");

You can't just use StringUtils.join(arr, " - "); because StringUtils doesn't have that overloaded version of method. Though, it has method StringUtils.join(int[], char).

Works at any Java version, from 1.2.


Using Java 8 streams:

Something like this:

Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = Arrays.stream(arr)
        .mapToObj(String::valueOf)
        .collect(Collectors.joining(" - "));

In fact, there are lot of variations to achieve it using streams.

Java 8's method String.join() works only with strings, so to use it you still have to convert int[] to String[].

Use with AIString[] sarr = Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new);
String result = String.join(" - ", sarr);

If you're stuck using Java 7 or earlier with no libraries, you could write your own utility method:

Use with AIpublic static String myJoin(int[] arr, String separator) {
    if (null == arr || 0 == arr.length) return "";

    StringBuilder sb = new StringBuilder(256);
    sb.append(arr[0]);

    //if (arr.length == 1) return sb.toString();

    for (int i = 1; i < arr.length; i++) sb.append(separator).append(arr[i]);

    return sb.toString();
}

Then you can do:

Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = myJoin(arr, " - ");
Answer from spirit on Stack Overflow
Top answer
1 of 9
93

Here's what I came up with. There are several ways to do this and they depend on the tools you are using.


Using StringUtils and ArrayUtils from Commons Lang:

Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = StringUtils.join(ArrayUtils.toObject(arr), " - ");

You can't just use StringUtils.join(arr, " - "); because StringUtils doesn't have that overloaded version of method. Though, it has method StringUtils.join(int[], char).

Works at any Java version, from 1.2.


Using Java 8 streams:

Something like this:

Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = Arrays.stream(arr)
        .mapToObj(String::valueOf)
        .collect(Collectors.joining(" - "));

In fact, there are lot of variations to achieve it using streams.

Java 8's method String.join() works only with strings, so to use it you still have to convert int[] to String[].

Use with AIString[] sarr = Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new);
String result = String.join(" - ", sarr);

If you're stuck using Java 7 or earlier with no libraries, you could write your own utility method:

Use with AIpublic static String myJoin(int[] arr, String separator) {
    if (null == arr || 0 == arr.length) return "";

    StringBuilder sb = new StringBuilder(256);
    sb.append(arr[0]);

    //if (arr.length == 1) return sb.toString();

    for (int i = 1; i < arr.length; i++) sb.append(separator).append(arr[i]);

    return sb.toString();
}

Then you can do:

Use with AIint[] arr = new int[] {1, 2, 3, 4, 5, 6, 7};
String result = myJoin(arr, " - ");
2 of 9
16

Java 8 Solution would be like this:

Use with AIStream.of(1,2,3,4).map(String::valueOf).collect(Collectors.joining("-"))
🌐
GeeksforGeeks
geeksforgeeks.org › java › ints-join-function-guava-java
Guava's Ints.join() Function in Java - GeeksforGeeks
July 11, 2025 - 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 ...
🌐
Blogger
javarevisited.blogspot.com › 2013 › 02 › combine-integer-and-string-array-java-example-tutorial.html
2 Ways to Combine Arrays in Java? Integer, String Array Copy Example
If you are combining arrays just as part of a programming exercise or homework then you better try to write a method yourself, to join two arrays. Also here is a nice diagram which shows the two common ways to combine or merge arrays in Java: Here is complete code example of combining two arrays in Java. You can write this combined method for any Java data type or object, I have used integer arrays for simplicity.
🌐
Baeldung
baeldung.com › home › java › java array › join array of primitives with separator in java
Join Array of Primitives with Separator in Java | Baeldung
January 8, 2024 - In this example, we also used the Ints.asList() method from Guava, which nicely transforms the array of primitives into a List of Integer. Guava offers a similar method for converting a char array to a List of Character. As a result, joining our char array looks very much like the above example that used the int array: String joined = Joiner.on(separator).join(Chars.asList(charArray)); Finally, if we can’t use either Java 8 or third-party libraries, we can manually join an array of elements with StringBuilder.
🌐
Mkyong
mkyong.com › home › java › java – how to join arrays
Java – How to join Arrays | mkyong.com
February 8, 2016 - package com.mkyong.example.array; import java.lang.reflect.Array; import java.util.Arrays; public class JoinArray { public static void main(String[] args) { String[] s1 = new String[]{"a", "b", "c"}; String[] s2 = new String[]{"d", "e", "f"}; String[] s3 = new String[]{"g", "h", "i"}; String[] result = joinArrayGeneric(s1, s2, s3); System.out.println(Arrays.toString(result)); int[] int1 = new int[]{1, 2, 3}; int[] int2 = new int[]{4, 5, 6}; int[] int3 = new int[]{7, 8, 9}; int[] result2 = joinArray(int1, int2, int3); System.out.println(Arrays.toString(result2)); } static <T> T[] joinArrayGener
🌐
Techie Delight
techiedelight.com › home › java › join two arrays in java
Join two arrays in Java | Techie Delight
July 7, 2026 - We know that Java 8 has IntStream to deal with primitive ints. The idea is to get IntStream of elements present in both arrays and concatenate them using IntStream.concat() method.
🌐
Medium
medium.com › javarevisited › nuanced-string-joining-in-java-7db547c331d0
Nuanced String Joining in Java | Javarevisited
April 23, 2024 - The String.join static method takes a CharSequence delimiter and either an array of CharSequence for the elements, or some kind of Iterable<CharSequence>. We can use the ArrayAdapter class from Eclipse Collections to convert the Integer array to an Iterable<CharSequence>.
🌐
Java2s
java2s.com › ref › java › java-array-join-int-array-to-string.html
Java array join int[] array to String
//package com.demo2s; public class ...ln(joinInts(tokens, delimiter)); }/*ww w. ja v a 2 s .co m*/ /** * Concatenates the given int[] array into one String, inserting a delimiter * between each pair of elements....
🌐
TutorialsPoint
tutorialspoint.com › ints-join-function-in-java
Ints join() function in Java
September 24, 2019 - import com.google.common.primitives.Ints; class Demo { public static void main(String[] args) { int[] myArr = { 10, 20, 30, 40, 50, 60,70, 80, 90, 100 }; System.out.println(Ints.join("-", myArr)); int index = Ints.indexOf(myArr, 40); if (index != -1) { System.out.println("Element 40 is in the array at position " + (index+1)); } else { System.out.println("Element 40 is not in the array"); } } }
Find elsewhere
🌐
Blogger
javarevisited.blogspot.com › 2013 › 02 › combine-integer-and-string-array-java-example-tutorial.html
Javarevisited: 2 Ways to Combine Arrays in Java? Integer, String Array Copy Example
In this Java example, we will first combine two int arrays and then, two String arrays. We will use Apache commons ArrayUtils.addAll() method to combine two integer arrays in Java and Google's Guava library to join two String arrays in Java.
🌐
Baeldung
baeldung.com › home › java › java array › concatenate two arrays in java
Concatenate Two Arrays in Java | Baeldung
1 month ago - However, since Java 5, the Collections utility class has introduced an addAll(Collection<? super T> c, T… elements) method. We can create a List object, then call this method twice to add the two arrays to the list.
🌐
Edureka Community
edureka.co › home › community › categories › java › join array elements with a separator in java
Join array elements with a separator in Java | Edureka Community
August 16, 2018 - Is there a way to join an array, i.e. perform opposite operation of what split() does. Iterating ... do it but not able to find it. Please suggest.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-merge-two-arrays
Java Program to Merge Two Arrays - GeeksforGeeks
August 19, 2026 - Java Streams provide a concise way to merge arrays. For integer arrays, we can use the IntStream.concat() method.
🌐
Codemia
codemia.io › home › knowledge hub › a quick and easy way to join array elements with a separator (the opposite of split) in java
A quick and easy way to join array elements with a separator (the opposite of split) in Java | Codemia
January 25, 2025 - 1import java.util.Arrays; 2import java.util.stream.Collectors; 3 4public class JoinNumbers { 5 public static void main(String[] args) { 6 Integer[] values = {10, 20, 30}; 7 8 String result = Arrays.stream(values) 9 .map(String::valueOf) 10 .collect(Collectors.joining(", ")); 11 12 System.out.println(result); // 10, 20, 30 13 } 14} This is also useful when you want filtering or transformation before joining.
🌐
javaspring
javaspring.net › blog › java-array-join
Java Array Join: A Comprehensive Guide — javaspring.net
To combine two arrays of the same type into one, you can manually create a new array with the appropriate size and copy the elements from the original arrays. Here is an example for integer arrays:
🌐
Baeldung
baeldung.com › home › java › java collections › join and split arrays and collections in java
Java - Join and Split Arrays and Collections | Baeldung
April 4, 2025 - @Test public void whenConvertCollectionToString_thenConverted() { Collection<String> animals = Arrays.asList("Dog", "Cat", "Bird", "Cow"); String result = animals.stream().collect(Collectors.joining(", ")); assertEquals(result, "Dog, Cat, Bird, Cow"); } Next, let’s create a String out of a Map. The process is very similar to previous examples, but here we have an extra step to first join each Map Entry: @Test public void whenConvertMapToString_thenConverted() { Map<Integer, String> animals = new HashMap<>(); animals.put(1, "Dog"); animals.put(2, "Cat"); animals.put(3, "Cow"); String result = animals.entrySet().stream() .map(entry -> entry.getKey() + " = " + entry.getValue()) .collect(Collectors.joining(", ")); assertEquals(result, "1 = Dog, 2 = Cat, 3 = Cow"); }
🌐
Programiz
programiz.com › java-programming › examples › concatenate-two-arrays
Java Program to Concatenate Two Arrays | Programiz
September 2, 2020 - import java.util.Arrays; public class Concat { public static void main(String[] args) { int[] array1 = {1, 2, 3}; int[] array2 = {4, 5, 6}; int aLen = array1.length; int bLen = array2.length; int[] result = new int[aLen + bLen]; System.arraycopy(array1, 0, result, 0, aLen); System.arraycopy(array2, 0, result, aLen, bLen); System.out.println(Arrays.toString(result)); } } ... In the above program, we've two integer arrays array1 and array2.