Java流,基于对象中的条件进行过滤,将值设置为字符串和数组
我不熟悉java流。尝试根据Java streams中的条件设置2个值。我试着用平面地图。可能是做错了什么 我想转换为streams的工作代码是:Java流,基于对象中的条件进行过滤,将值设置为字符串和数组,java,filter,java-stream,mapping,Java,Filter,Java Stream,Mapping,我不熟悉java流。尝试根据Java streams中的条件设置2个值。我试着用平面地图。可能是做错了什么 我想转换为streams的工作代码是: String NUME_AGENT = ""; for(int i = 0; i < reportInputOptionsExtsElemAgent.length; i++) { if(reportInputOptionsExtsElemAgent[i].getKey().equalsIgnoreCase(loadAGENT_ID)){
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())
};
}
}
您不需要平面图:
您不需要平面图:
可以在map方法内创建对象:
可以在map方法内创建对象:
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("");
String NUME_AGENT = Arrays.stream(reportInputOptionsExtsElemAgent)
//.flatMap(agent -> agent.stream) <---------- you don't need this
.filter(rgp-> rgp.getKey().equalsIgnoreCase(loadAGENT_ID))
.findFirst()
.map(rgp->rgp.getValue())
.orElse("");
reportInputOptionsExtsElemAgent = new ReportInputOptionsExt[]{new ReportInputOptionsExt(loadAGENT_ID, NUME_AGENT )};
ReportInputOptionsExt ext = Arrays.stream(reportInputOptionsExtsElemAgent)
.filter(rgp-> rgp.getKey().equalsIgnoreCase(loadAGENT_ID))
.findFirst()
.map(rgp->new ReportInputOptionsExt(loadAGENT_ID, rgp.getValue()))
.orElse(null);