Convert String[] to comma separated string in java

后端 未结 23 2238
暖寄归人
暖寄归人 2020-11-29 22:40

i have one String[]

String[] name = {\"amit\", \"rahul\", \"surya\"};

i want to send name as parameter in sql query inside IN

23条回答
  •  误落风尘
    2020-11-29 23:09

    As tempting and "cool" as the code may appear, do not use fold or reduce on large collections of strings, as these will suffer from the string concatenation problem

    String[] strings = { "foo", "bar", "baz" };
    Optional result = Arrays.stream(strings)
            .reduce((a, b) -> String.format("%s,%s", a, b));
    System.out.println(result.get());
    

    Instead, as per other answers, use String.join() if you already have a collection, or a StringBuilder if not.

提交回复
热议问题