Java: convert List to a String

前端 未结 22 2589
日久生厌
日久生厌 2020-11-22 01:03

JavaScript has Array.join()

js>[\"Bill\",\"Bob\",\"Steve\"].join(\" and \")
Bill and Bob and Steve

Does Java have anything

22条回答
  •  感情败类
    2020-11-22 01:51

    With a java 8 collector, this can be done with the following code:

    Arrays.asList("Bill", "Bob", "Steve").stream()
    .collect(Collectors.joining(" and "));
    

    Also, the simplest solution in java 8:

    String.join(" and ", "Bill", "Bob", "Steve");
    

    or

    String.join(" and ", Arrays.asList("Bill", "Bob", "Steve"));
    

提交回复
热议问题