Java function for arrays like PHP's join()?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
In Java, arrays are fundamental structures that store fixed-size sequential collections of elements of a type. However, unlike some other programming languages like PHP, Java does not have a built-in join() function in its standard library which directly converts arrays into a single string with elements separated by a specific delimiter. PHP's join() function, which is an alias of implode(), is widely used for concatenating the elements of an array into a string, separating them with a specified string delimiter. In Java, this functionality can still be achieved, but requires a more manual approach if not using Java 8 or newer features.
Implementing Join in Java (Pre-Java 8)
Before the introduction of Java 8, implementing a join() function equivalent required looping through the elements of an array and appending each to a StringBuilder, with a delimiter in between each element. Here's how you could achieve this:
You would call this function with an array and a string to use as a delimiter:
Java 8 and Beyond: Stream API
Java 8 introduced the Stream API, which significantly simplifies operations on collections, including arrays. The String.join() method and the Collectors.joining() collector are now the preferred ways to concatenate strings from an array or any collection.
Using String.join()
This static method from the String class is an efficient way to join array elements:
Using Streams with Collectors.joining()
If you're dealing with collections or you need more complex operations like filtering, you can use streams:
Tabular Summary of Methods
Here is a table summarizing the methods discussed:
| Method | Description | Java Version |
| Manual Loop | Build the string using a loop and StringBuilder. | Any |
String.join() | Use String class's static method for arrays and iterables. | Java 8+ |
Collectors.joining() | Use with the Stream API for collections and more complex operations. | Java 8+ |
Additional Considerations
When choosing an implementation, consider readability, performance, and what fits the use case:
- For a simple and clear solution,
String.join()is usually best. - For more control over elements (e.g., filtering or mapping before joining), the Stream API with
Collectors.joining()is more flexible. - In environments prior to Java 8, or for educational purposes, manually handling concatenation with loops and
StringBuilderprovides a deeper understanding of what these utility methods abstract away.
In conclusion, although Java does not provide a direct counterpart to PHP's join() out of the box for older versions, Java 8 and newer versions offer robust, concise, and expressive ways to accomplish the same task with added benefits of the Stream API. Whether for simple concatenations or more involved processing scenarios involving collections, Java provides the tools needed to build powerful string manipulation capabilities effectively.
Related reading
- Java Get first item from a collection
- Java Hashmap How to get key from value?
- Java HashMap.getObject infinite loop
- Java heap space - Out of memory error - Kafka Broker with SASL_SSL
- Java Future vs c async await
- Java FutureTask Exception
- Java heap terminology young, old and permanent generations?
- Java how can I split an ArrayList in multiple small ArrayLists?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.