gpt4 book ai didi

java - 使用 Java Streams 将对象列表转换为带过滤器的字符串列表

转载 作者:行者123 更新时间:2023-12-02 02:54:29 25 4
gpt4 key购买 nike

从对象列表中获取所有 estd 代码值,其中 iEflag 值为“E”。尝试过这个,但不起作用。

List<String> estdCodeList = applEstdList.stream()
.map(StdCode::getEstdCode)
.filter(x -> x.getiEflag().equals("E"))
.collect(Collectors.toList());

其中 applEstdListStdCode 类型的对象列表。

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());
}
}

最佳答案

您缺少另一个mapping,可能为:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com