简体   繁体   English

比较 Object 与 Map 键的列表并将列表值的值替换为 Map 值

[英]Compare list of Object with Map Key and Replace the Value of list value with Map Value

This is the class which has details,这是 class 有详细信息,

public class TestMap {


public static void main(String[] args) {
    // TODO Auto-generated method stub
    

    List<PersonData>  personDataLst = new ArrayList<PersonData>();
    
    Person person = new Person();
    Person person00 = new Person();
    
    person.setFirstName("AASH");
    person.setLastName("17072017");
    person00.setFirstName("LAKSH");
    person00.setLastName("11222020");
    
    PersonData personData = new PersonData();
    PersonData personData01 = new PersonData();
    personData01.setPerson(person00);
    personData.setPerson(person);
    personDataLst.add(personData01);
    personDataLst.add(personData);
    
    PersonResponse response = new PersonResponse();
    
    response.setPersonDatas(personDataLst);
    
    System.out.println(response.getPersonDatas().size());
    
    List<PersonData> list = response.getPersonDatas();
    
    Map<String , String> someHashMap = new HashMap<String , String>();
    someHashMap.put("17072017", "DateOfBirth");
    
    List<String> filteredLst = someHashMap.entrySet().stream()
            .filter(e -> list.contains(e.getKey()))
            .map(Map.Entry::getValue)
            .collect(Collectors.toList());
    System.out.println("******"+filteredLst);
}
}

I am trying to check whether the list contains map key, If match replace with the map value.我正在尝试检查列表是否包含 map 键,如果匹配替换为 map 值。 The code I have written does return an empty list.我编写的代码确实返回了一个空列表。

If the date of birth is stored in the last name property (which is a strange choice), the filtering should be as follows:如果出生日期存储在姓氏属性中(这是一个奇怪的选择),过滤应该如下:

List<String> filteredLst = someHashMap.entrySet().stream()
        .filter(e -> list.stream()
                         .map(p->p.getPerson().getLastName())
                         .anyMatch(lastname -> lastname.equals(e.getKey())))
        .map(Map.Entry::getValue)
        .collect(Collectors.toList());

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

相关问题 用 map 键值过滤 Object 列表,替换 Map 值 - Filter Object list with map key value , replace the Map value 转换地图 <Key, List<Value> &gt;到地图 <Key, Value> - Transforming Map<Key, List<Value>> to Map<Key, Value> 使用 Stream API 将 Map 值与列表进行比较 - Compare Map Value with list using Stream API 使用列表值Map反转地图 <Key, List<Value> &gt;到地图 <Value, Key> 在Java 8中 - Invert Map with list value Map<Key, List<Value>> to Map <Value, Key> in Java 8 转换列表<String>到地图<String, List<String> &gt; 以列表值为映射键,以空列表为映射值 java 8 - Convert List<String> to Map<String, List<String>> with list value as map key and map value with empty list java 8 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>> 如何在Java中将新键值添加到现有List Map对象 - How to add new key value to existing List Map object in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM