Please help me to utilize the new Java 8 features.
I have three arrays:
String[] firstnames = {"Aaa", "Bbb", "Ccc"};
String[] lastnames = {"Zzz", "Yyy", "Xxx"};
String[] mailaddresses = {"aaa@zzz.com", "bbb@yyy.com", "ccc@xxx.com"};
And want to use the new stream API to format the values into following string:
"firstname: %s\nlastname: %s\nmailaddress: %s\n"
An indirect approach with a stream of array indices is probably your best bet, given that there is no "zip" operation in the Streams API:
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.range;
...
range(0, firstnames.length)
.mapToObj(i-> String.format("firstname: %s\nlastname: %s\nmailaddress: %s\n",
firstnames[i], lastnames[i], mailaddresses[i]))
.collect(toList())
.forEach(System.out::print);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.