【发布时间】:2020-08-27 04:48:27
【问题描述】:
我是 Java 流的新手。尝试根据 Java 流中的条件设置 2 个值。我尝试使用平面图。可能是做错了什么。
我想转换为流的工作代码是:
String NUME_AGENT = "";
for(int i = 0; i < reportInputOptionsExtsElemAgent.length; i++) {
if(reportInputOptionsExtsElemAgent[i].getKey().equalsIgnoreCase(loadAGENT_ID)){
NUME_AGENT = reportInputOptionsExtsElemAgent[i].getValue();
reportInputOptionsExtsElemAgent =
new ReportInputOptionsExt[] {
new ReportInputOptionsExt(loadAGENT_ID,
reportInputOptionsExtsElemAgent[i].getValue())
};
}
}
我的尝试:
String NUME_AGENT =
Arrays.stream(reportInputOptionsExtsElemAgent) // not sure about this
.flatMap(agent -> agent.stream) // not sure about this
.filter(rgp-> rgp.getKey().equalsIgnoreCase(loadAGENT_ID))
.findFirst()
.map(rgp->rgp.getValue())
.orElse("");
【问题讨论】:
标签: java filter java-stream mapping