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(" "));