[英]java8 Stream API filter Map based on arrayList
Class Person {
Map<String, String> personNature = new HashMap<String,String>();
// Getter and Setter of Map
public void setBehaviour(String key, String value) {
personNature.put(key, value);
}
}
main(String[] abc) {
Person p = new Person();
Map<String, String> personAllNature = new HashMap<String, String>();
allNatures.put("personName", "Raj");
allNatures.put("personAggresive", "8");
allNatures.put("personShyness", "5");
allNatures.put("personCourage", "9");
allNature.put("personFakness", "2");
List<String> personVisibleAttributes= new ArrayList<String>();
personVisibleAttributes.add("personAggresive");
personVisibleAttributes.add("personShyness");
}
现在,我想以一种方式过滤Map(personAllNature),以便在我要设置Person's Map时,我只会从Map中的List(personVisibleAttributes)中获取那些值。
如下所示:
personAllNature.entrySet().parallelStream()
.filter(map -> personVisibleAttributes.contains(map.getKey()))
.forEach(entry -> p.setBehaviour( entry.getKey(), entrygetValue())
总体 :
根据列表中的值过滤列表,其中地图键位于列表中,然后在Person实例中设置所有这些地图值。
因为只添加List
的attributes
,所以可以直接从List
进行迭代:
personVisibleAttributes.forEach(k -> p.setBehaviour(k, personAllNature.get(k));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.