Why can't I map integers to strings when streaming from an array?

前端 未结 5 1840
孤街浪徒
孤街浪徒 2020-12-04 17:00

This code works (taken in the Javadoc):

List numbers = Arrays.asList(1, 2, 3, 4);
String commaSeparatedNumbers = numbers.stream()
    .map(i -         


        
5条回答
  •  醉话见心
    2020-12-04 18:03

    You can create an Integer Stream using Arrays.stream(int[]) , you can call mapToObj like mapToObj(Integer::toString).

    String csn = Arrays.stream(numbers) // your numbers array
    .mapToObj(Integer::toString)
    .collect(Collectors.joining(", "));
    

    Hope this helps..

提交回复
热议问题