简体   繁体   English

用 map 键值过滤 Object 列表,替换 Map 值

[英]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 持有一个键值,并基于此需要为列表应用过滤器。

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) .应用过滤条件:如果personLst.contains(ADL)这是一个 map 密钥,过滤它并用 map 值(12345)替换 zip。

Tried different stackoverflow question, did not help much on.尝试了不同的stackoverflow问题,并没有太大帮助。

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?真的是指有效 java 对象的列表,对吧? Because in the current state those would be valid JavaScript Objects, but not valid Java objects.因为在当前的 state 中,这些将是有效的 JavaScript 对象,但不是有效的 Java 对象。

Also, should a List<Person> really be called with personLst.contains(String) ?另外, List<Person>真的应该用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因此,假设这些是具有正确 getter 和 setter 的有效 Java 对象,并且打算使用正确的方法,您应该能够使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较 Object 与 Map 键的列表并将列表值的值替换为 Map 值 - Compare list of Object with Map Key and Replace the Value of list value with Map Value 如何过滤和检查列表中特定键和值是否存在列表<map<string, object> > 在 java </map<string,> - How to filter and check if specific key and value exists list in List<Map<String, Object>> in java 映射与键值相同的对象 - Map with same object as key value 转换地图 <Key, List<Value> &gt;到地图 <Key, Value> - Transforming Map<Key, List<Value>> to Map<Key, Value> Java 8列表 <Map<String, Object> &gt;列表 <Map<String, Object> &gt;按键分组并按值计数 - Java 8 List<Map<String, Object>> to List<Map<String, Object>> group by key and count by value 如何从列表中删除键/值对<Map<String, Object> &gt; - How to remove the key/value pair from a List<Map<String, Object>> 如何使用 Map 键作为列表 object 和值作为顺序对 ArrayList 进行排序? - How to sort an ArrayList using a Map key as list object and value as the order? 将键值对添加到列表<map<string, object> &gt; </map<string,> - Add key value pair to List<Map<String, Object>> 映射替换了列表对象的所有相同键值 - Map replaced all the same key value of list object 如何在Java中将新键值添加到现有List Map对象 - How to add new key value to existing List Map object in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM