How to change the value associated with a key in a list of HashMaps that have same Keys?
我的输入中有一个共有13个HashMaps的列表。 每个HashMap只有2个键"fieldName"和"accessibilityType"以及相应的值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | "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" } ] |
我试图迭代这个并将"accessibilityType"的值更改为"READ",其中"fieldName"是"birthDate"。 有人可以说有效的方法来做到这一点。 这是我到目前为止只读取和打印每个键值对的尝试:
1 2 3 4 5 6 7 8 9 10 11 |
你可以这样做,
1 2 3 4 5 |
浏览每个地图,同时将其映射到新地图。 键保持不变,只有值可能会改变。 如果映射包含键
可以使用JDK-8中的
1 2 3 4 | list.forEach(map -> { String fieldName = map.get("fieldName"); if("birthDate".equals(fieldName)) map.put("accessibilityType","READ"); }); |