如何获取id = 2的所有元素?
以下代码仅提供最后一个,即channelMetadataDetail3
:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class APICheck {
public static void main(String[] args) {
List<ChannelMetadataDetails> channelMetadataDetails =
new ArrayList<ChannelMetadataDetails>();
ChannelMetadataDetails channelMetadataDetail1 =
new ChannelMetadataDetails();
channelMetadataDetail1.setId("2");
channelMetadataDetail1.setUnitOfMeasureId("3");
ChannelMetadataDetails channelMetadataDetail2 =
new ChannelMetadataDetails();
channelMetadataDetail2.setId("2");
channelMetadataDetail2.setUnitOfMeasureId("3");
channelMetadataDetail2.setMeasureTypeId("5");
ChannelMetadataDetails channelMetadataDetail3 =
new ChannelMetadataDetails();
channelMetadataDetail3.setId("2");
channelMetadataDetails.add(channelMetadataDetail1);
channelMetadataDetails.add(channelMetadataDetail2);
channelMetadataDetails.add(channelMetadataDetail3);
channelMetadataDetails =channelMetadataDetails.
stream().filter(cm -> cm.getId().equals("2")).
filter(cm -> cm.getUnitOfMeasureId().equals(null)).
filter(cm -> cm.getMeasureTypeId().equals(null)).
collect(Collectors.toList());
}
}
注意: - 如果cm.getUnitOfMeasureId()
为空,那么我将获得NullPointerException
更新
简而言之,我只想要那些元素非空的对象。
例如使用for循环:
for (ChannelMetadataDetails cmEach : channelMetadataDetails) {
if(cmEach.getId() != null && cmEach.getUnitOfMeasureId() == null && cmEach.getMeasureTypeId() == null) {
channelMetadataDetails.stream().filter(cm -> cm.getId().equals("2")).collect(Collectors.toList());
} else if(cmEach.getId() == null && cmEach.getUnitOfMeasureId() != null && cmEach.getMeasureTypeId() == null) {
channelMetadataDetails.stream().filter(cm -> cm.getUnitOfMeasureId().equals("4")).collect(Collectors.toList());
} else if(cmEach.getId() == null && cmEach.getUnitOfMeasureId() == null && cmEach.getMeasureTypeId() != null) {
channelMetadataDetails.stream().filter(cm -> cm.getMeasureTypeId().equals("5")).collect(Collectors.toList());
} else if(cmEach.getId() != null && cmEach.getUnitOfMeasureId() != null && cmEach.getMeasureTypeId() == null) {
channelMetadataDetails.stream().filter(cm -> cm.getId().equals("2") && cm.getUnitOfMeasureId().equals("4")).collect(Collectors.toList());
} else if(cmEach.getId() != null && cmEach.getUnitOfMeasureId() == null && cmEach.getMeasureTypeId() != null) {
channelMetadataDetails.stream().filter(cm -> cm.getId().equals("2") && cm.getMeasureTypeId().equals("5")).collect(Collectors.toList());
} else if(cmEach.getId() != null && cmEach.getUnitOfMeasureId() != null && cmEach.getMeasureTypeId() != null) {
channelMetadataDetails.stream().filter(cm -> cm.getId().equals("2") && cm.getUnitOfMeasureId().equals("4") && cm.getMeasureTypeId().equals("5")).collect(Collectors.toList());
}
}
答案 0 :(得分:1)
您只获得最后一个元素的原因是您的第二个和第三个过滤器要求cm.getUnitOfMeasureId()
和cm.getMeasureTypeId()
为空。这只适用于最后一个元素。如果您只想按getId()
进行过滤,请删除其他过滤条件:
channelMetadataDetails =
channelMetadataDetails.stream()
.filter(cm -> cm.getId() != null && cm.getId().equals("2"))
.collect(Collectors.toList());
编辑:
根据您的编辑,您希望3个属性中至少有一个具有值,并且您想要检查所有非空值:
channelMetadataDetails =
channelMetadataDetails.stream()
.filter(cm -> cm.getId() != null || cm.getUnitOfMeasureId() != null || cm.getMeasureTypeId() != null)
.filter(cm -> cm.getId() == null || cm.getId().equals("2"))
.filter(cm -> cm.getUnitOfMeasureId() == null || cm.getUnitOfMeasureId().equals("4"))
.filter(cm -> cm.getMeasureTypeId() == null || cm.cm.getMeasureTypeId().equals("5"))
.collect(Collectors.toList());