Implement PHP implode function in Java


I implemented something like PHP's implode function in Java.

(Addition) For Java8, [join]( There seems to be a function called Iterable-). @nishemon Thank you for your comment!


 * Join list elements with a string
 * @param stringList The list of strings to implode.
 * @param glue The String for joining list.
 * @return Returns a string containing a string representation of all the list elements
 *          in the same order, with the glue string between each element.
public static String implode(List<String> stringList, String glue) {
    StringBuilder stringBuilder = new StringBuilder();
    int index = 0;
    int size = stringList.size();

    for (String value : stringList) {

        if (index < size - 1) {


    return stringBuilder.toString();

(Addition) I was taught a general-purpose implementation. @shiracamus Thank you for your comment!

public static String implode(Iterable<? extends CharSequence> values, CharSequence glue) {
    StringBuilder buffer = new StringBuilder();
    CharSequence separator = "";
    for (CharSequence value : values) {
        separator = glue;
    return buffer.toString();

(Addition) I was taught the implementation again. @ saka1029 Thank you for your comment!

public static <T> String implode(List<T> list, String glue) {
    StringBuilder sb = new StringBuilder();
    for (T e : list) {
    return sb.substring(glue.length());


Only one simple one for the time being

public void testImplode() {
    ArrayList<String> list = new ArrayList<>();

    String glue = " ";
    String actual = Util.implode(list, glue);

    String expected = "A B C";
    assertEquals(expected, actual);

About PHP ʻimplode ()`

Looking at the PHP manual, it says:

Note: implode () can accept arguments in either order for historical reasons. However, from the point of view of consistency with explode (), it would be less confusing to use the order of the arguments described in the documentation.

Also, in PHP, the default of glue is an empty string, but this time it is not considered. If you do, it may be good to make something overloaded like this.

public static String implode(List<String> stringList) {
    return implode(stringList, "");


Please let me know if there is something wrong or a better way! : pray:


Recommended Posts

Implement PHP implode function in Java
Implement application function in Rails
Implement follow function in Rails
Implement two-step verification in Java
Implement Basic authentication in Java
Implement math combinations in Java
2 Implement simple parsing in Java
Implement Email Sending in Java
Implement functional quicksort in Java
Implement rm -rf in Java.
Implement XML signature in Java
Implement Table Driven Test in Java 14
3 Implement a simple interpreter in Java
Implement simple login function in Rails
Implement tagging function in form object
Implement the same function as C, C ++ system ("cls"); in Java
Try to implement Yubaba in Java
Implement CSV download function in Rails
1 Implement simple lexical analysis in Java
Implementation of like function in Java
How to implement date calculation in Java
How to implement Kalman filter in Java
Implement API Gateway Lambda Authorizer in Java Lambda
Differences in writing in Ruby, PHP, Java, JS
Try to implement n-ary addition in Java
How to implement coding conventions in Java
Immutable (immutable) List object conversion function in Java8
Implement something like a stack in Java
Partization in Java
Changes in Java 11
Rock-paper-scissors in Java
Pi in Java
FizzBuzz in Java
[Java] Implement a function that uses a class implemented in the Builder pattern
Implement star rating function using Raty in Rails6
I tried to implement deep learning in Java
Encrypt / decrypt with AES256 in PHP and Java
Interpreter implementation in Java
Make Blackjack in Java
Rock-paper-scissors app in Java
Constraint programming in Java
Put java8 in centos7
NVL-ish guy in Java
"Hello World" in Java
Callable Interface in Java
I tried to implement Firebase push notification in Java
[Rails] Implement credit card registration / deletion function in PAY.JP
Comments in Java source
Azure functions in java
[Rails] Implement search function
Implement user follow function in Rails (I use Ajax) ②
Format XML in Java
Simple htmlspecialchars in Java
Boyer-Moore implementation in Java
Hello World in Java
I tried to make a login function in Java
Use OpenCV in Java
webApi memorandum in java
Type determination in Java
Quickly implement a singleton with an enum in Java
Ping commands in Java