【发布时间】:2022-10-15 00:06:04
【问题描述】:
我想将以下带有if-statement 的for-loop 转换为Stream。
public Integer months() {
String desiredObject = // initialize somehow
Map<String, Integer> monthDays = new HashMap<>();
monthDays.put("March", 31);
monthDays.put("April", 30);
Set<Map.Entry<String, Integer>> entrySet = monthDays.entrySet();
for (Map.Entry<String, Integer> pair : entrySet) {
if (desiredObject.equals(pair.getKey())) {
return pair.getValue();
}
}
}
怎么做?
【问题讨论】:
标签: java for-loop if-statement java-stream