Convert an array of Integer into a single String

LostReality :

I am having trouble finding how to easily build a string from an arrayList of Integer with Java 8, just like this :

[3, 22, 1, 5] to "3 22 1 5"

For the moment I tried :

List<Integer> ids = new ArrayList<Integer>();
/* ... */
String.join(" ", ((ArrayList<String>)(ids))); //cast do not work

List<Integer> ids = new ArrayList<Integer>();
/* ... */
String.join(" ", ids.forEach(id -> Integer.toString(id))); //forEach returns void so it throws an error

Anyone does have a convenient/elegant solution ?

Thanks anyone & have a nice day

purring pigeon :

You can do this using Streams

List<Integer> ids = new ArrayList<Integer>();
/* ... */
String joined= ids.stream()
                   .map(i -> i.toString())
                   .collect(Collectors.joining(" "));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=141677&siteId=1