[英]filter the objects with filter in the nested collection?
DeviceType.java设备类型.java
private String Id;
private List<DeviceTelemetry> telemetries = new ArrayList<>()
//Getter Setter
DeviceTelemetry.java (Enum Class) DeviceTelemetry.java(枚举类)
private String name;
private String description;
//Getter Setter
I want to fetch all DeviceType Id , which have DeviceTelemetry name start with "System"(String).我想获取所有DeviceType Id ,其中DeviceTelemetry名称以“System”(字符串)开头。
I did something like this:-我做了这样的事情: -
List<DeviceType> filteredStartWithDeviceType =deviceTypes.stream().
filter(f -> f.getTelemetries().stream()
.anyMatch(g -> g.getName().startsWith("System"))).collect(Collectors.toList());
Can Anyone tell me how can I get DeviceType Id from this?谁能告诉我如何从中获取DeviceType Id ?
You just need to map
the values from DeviceType
to String
您只需要map
从DeviceType
到String
的值
List<String> filteredStartWithDeviceType = deviceTypes.stream()
.filter(f -> f.getTelemetries().stream()
.anyMatch(g -> g.getName().startsWith("System")))
.map(DeviceType::getId) // or .map(dt -> dt.getId()) if you prefer this syntax
.collect(Collectors.toList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.