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");
}
Now i want to filter Map (personAllNature ) in such a way that when i want to set Person's Map i will get only those values from Map which is in List(personVisibleAttributes)
something like below :
personAllNature.entrySet().parallelStream()
.filter(map -> personVisibleAttributes.contains(map.getKey()))
.forEach(entry -> p.setBehaviour( entry.getKey(), entrygetValue())
Overall :
filter a Map based on values in List where Map key is in List and then set all those map value in Person instance.
因为只添加List
的attributes
,所以可以直接从List
进行迭代:
personVisibleAttributes.forEach(k -> p.setBehaviour(k, personAllNature.get(k));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.