Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
58 views
in Technique[技术] by (71.8m points)

java - Filter Object list with map key value , replace the Map value

I have a List of Objects

List<Person> personLst = [{"person:" {personName:AASH_01 , country :AUS, state :ADL, zip :null }, 
                {personName:AASH_01 , country :AUS, state :MLB, zip :null}}] 

and Map holds a key value and based on this need to apply the filter for the list.

Map<String, String> lstMap = new HashMap<>();
lstMap.put("ADL","12345")

Apply Filter Condition: If the personLst.contains(ADL) this is a map key filter it and replace the zip with map value(12345).

Tried different stackoverflow question , did not help much on .

question from:https://stackoverflow.com/questions/65910744/filter-object-list-with-map-key-value-replace-the-map-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Just to be sure,

List<Person> personLst = [{"person:" {personName:AASH_01 , country :AUS, state :ADL, zip :null },{personName:AASH_01 , country :AUS, state :MLB, zip :null}}]

really refers to a list of valid java objects, right? Because in the current state those would be valid JavaScript Objects, but not valid Java objects.

Also, should a List<Person> really be called with personLst.contains(String)?

So, assuming these are valid Java objects with the correct getters and setters, and the correct method is intended, you should be able to use

personLst.forEach(p -> {
if(lstMap.containsKey(p.getState()))
p.setZip(lstMap.get(p.getState()));
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...