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 = {"[email protected]", "[email protected]", "[email protected]"};

And want to use the new stream API to format the values into following string:

"firstname: %s\nlastname: %s\nmailaddress: %s\n"
1

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.