从对象列表中获取所有 estd 代码值,其中 iEflag
值为“E”。尝试过这个,但不起作用。
List<String> estdCodeList = applEstdList.stream()
.map(StdCode::getEstdCode)
.filter(x -> x.getiEflag().equals("E"))
.collect(Collectors.toList());
其中 applEstdList
,StdCode
类型的对象列表。
public class StdCode implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private String applnCode;
private String estdCode;
private String iEflag;
}
我试图使用 Java Streams 转换此代码。在这里使用 Stream 有任何性能优势吗?
List<String> iEflagIsE = new ArrayList<String>();
List<String> iEflagIsNotE = new ArrayList<String>();
//Creating the respective exclusion inclusion list
for(ApplicationEstCode applnList :applEstList){
if(applnList.getiEflag().equals("E")){
iEflagIsE.add(applnList.getEstCode());
}else{
iEflagIsNotE.add(applnList.getEstCode());
}
}
最佳答案
您缺少另一个map
ping,可能为:
List<String> stdCodeList = applEstdList.stream()
.map(StdCode::getStdCode)
.map(a -> a.getiEflag())
.filter(x -> x.equals("E"))
.collect(Collectors.toList());
或者将map
操作组合到:
List<String> stdCodeList = applEstdList.stream()
.map(applEstd -> applEstd.getStdCode().getiEflag())
.filter(iEflag -> iEflag.equals("E"))
.collect(Collectors.toList());
关于java - 使用 Java Streams 将对象列表转换为带过滤器的字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57086723/