简体   繁体   English

如何更改与具有相同键的HashMap列表中的键关联的值?

[英]How to change the value associated with a key in a list of HashMaps that have same Keys?

I have one List of totally 13 HashMaps in my input. 我的输入中只有一个列表,共13个HashMap。 Each HashMap has only 2 Keys "fieldName" and "accessibilityType" and corresponding values: 每个HashMap仅具有2个键“ fieldName”和“ accessibilityType”以及相应的值:

"fieldAccessibility": [
    {
        "fieldName": "firstName",
        "accessibilityType": "EDITABLE"
    },
    {
        "fieldName": "lastName",
        "accessibilityType": "EDITABLE"
    },
    {
        "fieldName": "avatarUrl",
        "accessibilityType": "EDITABLE"
    },
    {
        "fieldName": "username",
        "accessibilityType": "EDITABLE"
    },
    {
        "fieldName": "birthDate",
        "accessibilityType": "EDITABLE"
    },
    {
        "fieldName": "phoneNumbers",
        "accessibilityType": "EDITABLE"
    },
    {
        "fieldName": "email",
        "accessibilityType": "EDITABLE"
    },
    {
        "fieldName": "language",
        "accessibilityType": "EDITABLE"
    },
    {
        "fieldName": "externalId",
        "accessibilityType": "EDITABLE"
    },
    {
        "fieldName": "externalCode",
        "accessibilityType": "EDITABLE"
    },
    {
        "fieldName": "punchBadgeId",
        "accessibilityType": "EDITABLE"
    },
    {
        "fieldName": "minor",
        "accessibilityType": "EDITABLE"
    },
    {
        "fieldName": "seniorityDate",
        "accessibilityType": "EDITABLE"
    }
]

I am trying to iterate through this and change the value of "accessibilityType" into "READ" where "fieldName" is "birthDate". 我正在尝试遍历此过程,并将“ accessibilityType”的值更改为“ READ”,其中“ fieldName”是“ birthDate”。 Can someone please say the efficient way to do this. 有人可以说一下这样做的有效方法。 This is my attempt so far to just read and print each key-value pair: 到目前为止,这是我尝试读取和打印每个键值对的尝试:

final List<HashMap<String, String>> list = some code to get the input;
    for (HashMap<String, String> m : list)
    {
        for (HashMap.Entry<String, String> e : m.entrySet())
        {
            String key = e.getKey();
            String value = e.getValue();

            System.out.println("SEE HERE TEST " + key + " = " + value);

        }}

it can be done as follows using forEach as of JDK-8: 从JDK-8开始,可以使用forEach如下进行操作:

list.forEach(map -> {
      String fieldName = map.get("fieldName");
      if("birthDate".equals(fieldName)) map.put("accessibilityType", "READ");
});

You can do it like so, 你可以这样做

List<Map<String, String>> updatedMaps = list.stream()
        .map(m -> m.entrySet().stream().collect(Collectors.toMap(
        Map.Entry::getKey,
        e -> m.containsValue("birthDate") && e.getKey().equals("accessibilityType") ? "READ" : e.getValue())))
        .collect(Collectors.toList());

Go through each map while mapping it into a new one. 浏览每张地图,同时将其映射到新地图。 The keys remain the same and only values may change. 键保持不变,只有值可以更改。 If your map contains the key birthDate and if the current entry's key is accessibilityType , then incorporate READ as the new value for that map entry. 如果您的地图包含键birthDate并且当前条目的键是accessibilityType ,则将READ合并为该地图条目的新值。 Otherwise leave the existing value as it is. 否则,请保留现有值。 Finally collect all the new maps into a result container. 最后,将所有新地图收集到结果容器中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM