Create formatted string from ArrayList

前端 未结 7 955
隐瞒了意图╮
隐瞒了意图╮ 2021-01-04 20:11

Consider following code:

    ArrayList aList = new ArrayList();
    aList.add(2134);
    aList.add(3423);
    aList.add(4234);
         


        
7条回答
  •  余生分开走
    2021-01-04 21:13

    Building off Mateusz's Java 8 example, there's an example in the StringJoiner JavaDoc that nearly does what OP wants. Slightly tweaked it would look like this:

    List numbers = Arrays.asList(1, 2, 3, 4);
    
    String commaSeparatedNumbers = numbers.stream()
         .map(i -> i.toString())
         .collect( Collectors.joining(",","(",")") );
    

提交回复
热议问题