[英]How to map one string into another in stream?
I have a big problem with returning the following in-stream strings. 我在返回以下流内字符串时遇到了很大的问题。 Will you help?
你会帮忙吗?
Here is my list: 这是我的清单:
List<String> dest = Arrays.asList(
"bleble bleble 2000",
"WAW HAV 1200",
"xxx yyy 789",
"WAW DPS 2000",
"WAW HKT 1000"
);
How can I return strings like this: //to HKT - price in EUR: 4300 //to HAV - price in EUR: 5160 //to DPS - price in EUR: 8600
如何返回这样的字符串:
//to HKT - price in EUR: 4300 //to HAV - price in EUR: 5160 //to DPS - price in EUR: 8600
in on stream? 在直播中?
enter codedouble ratePLNvsEUR = 4.30;
List<String> result = dest
.stream()
.filter(x -> x.startsWith("WAW"))
//.map() <- how to change "WAW HAV 1200" into "HAV - price in EUR: 5160"
.collect(Collectors.toList());
for (String r : result) System.out.println(r);
This for (..) is required. 这是(..)的必填项。
You can use map()
to spli string, and get needed elements (second and third): 您可以使用
map()
分割字符串,并获取所需的元素(第二和第三):
List<String> result = dest
.stream()
.filter(x -> x.startsWith("WAW"))
.map(x -> {
String[] row = x.split(" ");
return String.format("to %s - price in EUR: %s",
row[1],
Integer.parseInt(row[2]) * ratePLNvsEUR);
})
.collect(Collectors.toList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.