请帮助我使用新的Java 8功能。
我有三个阵列:
String[] firstnames = {"Aaa", "Bbb", "Ccc"};
String[] lastnames = {"Zzz", "Yyy", "Xxx"};
String[] mailaddresses = {"aaa@zzz.com", "bbb@yyy.com", "ccc@xxx.com"};
并希望使用新的流API将值格式化为以下字符串:
"firstname: %s\nlastname: %s\nmailaddress: %s\n"
答案 0 :(得分:2)
对于数组索引流的间接方法可能是你最好的选择,因为没有" zip" 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);