Stream.of(results).limit(results.length-2).collect(Collectors.joining())
Answer from mtj on Stack Overflow Top answer 1 of 2
3
Stream.of(results).limit(results.length-2).collect(Collectors.joining())
2 of 2
1
You could turn it around:
if (results.length >= 3) {
Deque<String> resultList = new ArrayDeque<>(Arrays.asList(results));
order.setAddress(resultList.removeLast());
order.setPhoneNumber(resultList.removeLast());
order.setConsignee(String.join("", resultList));
}
CodeGym
codegym.cc › java blog › strings in java › java string join() method
Java String join() Method
January 8, 2025 - Iterable which can be a list or array of elements, needs to be joined. It always returns a concatenated string of elements joined by the delimiter. import java.util.ArrayList; class Main { public static void main(String[] args) { ArrayList<String> arrayOfStrings = new ArrayList<>(); arrayOfStrings.add("Concatenating"); arrayOfStrings.add("the"); arrayOfStrings.add("array"); arrayOfStrings.add("list"); arrayOfStrings.add("elements"); arrayOfStrings.add("with"); arrayOfStrings.add("space"); arrayOfStrings.add("as"); arrayOfStrings.add("delimiter"); // joining arrayList with space between them as delimiter String joinedStr = String.join(" ", arrayOfStrings); System.out.println(joinedStr); } }
01:41
Java Programming Series Episode 111: String.join() - YouTube
05:38
How to join Strings with a delimeter using the Java join() method ...
08:44
Java Interview: Join Two Arrays in Java || Simple and Powerful ...
02:47
#72 Java String join() Method – Combine Strings with a Delimiter ...
08:23
Explain join() method of String Class in Java (Core Java Interview ...
02:41
2 ways to join strings in java | String.join() & StringJoiner java ...
Educative
educative.io › answers › what-is-stringutilsjoin-in-java
What is StringUtils.join() in Java?
public static String join(final Object[] array, final char delimiter, final int startIndex, final int endIndex) final Object[] array: The array of values to join. final char delimiter: The delimiter character to use. final int startIndex: The ...
Top answer 1 of 15
970
Using Java 8 you can do this in a very clean way:
String.join(delimiter, elements);
This works in three ways:
1) directly specifying the elements
String joined1 = String.join(",", "a", "b", "c");
2) using arrays
String[] array = new String[] { "a", "b", "c" };
String joined2 = String.join(",", array);
3) using iterables
List<String> list = Arrays.asList(array);
String joined3 = String.join(",", list);
2 of 15
326
If you're on Android you can TextUtils.join(delimiter, tokens)
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 - In this quick tutorial, we’ll learn how to join and to split Arrays and Collections in Java, making good use of the new stream support. Let’s start by joining two Arrays together using Stream.concat: @Test public void whenJoiningTwoArrays_thenJoined() { String[] animals1 = new String[] { "Dog", "Cat" }; String[] animals2 = new String[] { "Bird", "Cow" }; String[] result = Stream.concat( Arrays.stream(animals1), Arrays.stream(animals2)).toArray(String[]::new); assertArrayEquals(result, new String[] { "Dog", "Cat", "Bird", "Cow" }); } Let’s do the same join with two Collections: @Test publ
Top answer 1 of 5
2
You may use a StringJoiner for that solution.
List<String> mainList = new ArrayList<>(); //The one to contain all Strings from yourList
List<String> yourList = Arrays.asList("Hey", "how", "are", "you"); //The list of Strings
StringJoiner joiner = new StringJoiner(" "); //Space as delimiter, strings will be separated with spaces
for (String s : yourlist) {
joiner.add(s);
}
mainList.add(joiner.toString()); //Add the new concatenated String to your list, in a single string
2 of 5
2
Java 8 added a StringJoiner class which does exactly what you want (the following code is in Java 9):
List<String> list = List.of("hey", "how", "are", "you");
StringJoiner joiner = new StringJoiner(" ");
list.forEach(str -> joiner.add(str));
System.out.println(joiner.toString()); // hey how are you
// and if you want the final result to be in a list
// Java 9:
list = List.of(joiner.toString());
// anything before Java 9
list = Arrays.asList(joiner.toString());
Another (simpler) way to do it (since Java 8):
String.join(" ", list)
BeginnersBook
beginnersbook.com › 2017 › 10 › java-string-join-method
Java String join() method
June 4, 2024 - public class StringJoinExample { public static void main(String[] args) { // Here , is a delimiter and elements are strings String result = String.join(", ", "Cricket", "Hockey", "Tennis"); System.out.println(result); // Output: "Cricket, Hockey, Tennis" } } import java.util.Arrays; import java.util.List; public class StringJoinExample { public static void main(String[] args) { List<String> sports = Arrays.asList("Cricket", "Hockey", "Tennis"); // Here, we are joining elements of an ArrayList with // comma as delimiter String result = String.join(", ", sports); System.out.println(result); // Output: "Cricket", "Hockey", "Tennis" } }
TutorialKart
tutorialkart.com › java › how-to-join-elements-of-string-array-with-delimiter-in-java
How to Join Elements of String Array with Delimiter in Java?
December 24, 2020 - To join elements of given string array strArray with a delimiter string delimiter, use String.join() method.
Tutorjoes
tutorjoes.in › java_programming_materials › joining_strings_with_a_delimiter
Joining Strings with a delimiter and Reversing Strings
String word = "world"; System.out.println(word); char[] array = word.toCharArray(); for (int index = 0, mirroredIndex = array.length - 1; index < mirroredIndex; index++, mirroredIndex--) { char temp = array[index]; array[index] = array[mirroredIndex]; array[mirroredIndex] = temp; } // print reversed System.out.println(new String(array)); Previous · Next · Creating Your First Java Program ·
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 - Use String.join for String[] and other string collections. Use streams plus Collectors.joining for object arrays that need conversion.
Vultr Docs
docs.vultr.com › java › standard library › java › lang › string › join()
Java String join() - Concatenate Strings | Vultr Docs
December 27, 2024 - When the join() method encounters null values in the items array, it treats them as "null" strings in the output, resulting in "null, Toaster, null, Microwave". The String.join() method in Java provides a powerful and easy way to concatenate multiple strings with a specified delimiter.
Top answer 1 of 6
1
@Taywin · : I think you meant your loop to be :for (int i = 1; i < array[index]; i++ · and NOTfor (int i = 1; i <= array[index]; i++) · Why inclusive? Pseudo code syntax?
2 of 6
0
@NP-complete · , the word inclusive in pseudo code means you do not include the value in your range. What I said means [1, array_size). If it is exclusive, it means the value will be included in the range.
DEV Community
dev.to › lifeportal20002010 › 4-ways-to-concatenate-arrays-in-java-from-systemarraycopy-to-stream-api-2a4k
4 Ways to Concatenate Arrays in Java: From System.arraycopy to Stream API - DEV Community
February 8, 2026 - System.arraycopy is the most fundamental and high-performance way to join arrays. While the syntax is a bit verbose, it works for both primitive types (int, byte, etc.) and reference types (String, Object). import java.util.Arrays; public class ArrayCopyExample { public static void main(String[] args) { int[] array1 = { 1, 2, 3 }; int[] array2 = { 4, 5, 6 }; // 1. Calculate and allocate the size for the new array int[] result = new int[array1.length + array2.length]; // 2. Copy array1 to the beginning (index 0) of result System.arraycopy(array1, 0, result, 0, array1.length); // 3.
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.
Medium
medium.com › @cherukurisankar › stringjoiners-in-java-8-e1e97c195e32
Different ways to join the strings in Java | by Sivasankar Cherukuri | Medium
June 4, 2024 - Here is what the new Java class says Here. StringJoiner is used to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix. There are two constructors available from the StringJoiner class