0

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"

1 Answers1

2

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);
William F. Jameson
  • 1,833
  • 9
  • 14