猿问

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

我的输入中只有一个列表,共13个HashMap。每个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"

    }

]

我试图通过它进行迭代,并将“ accessibilityType”的值更改为“ READ”,其中“ fieldName”是“ birthDate”。有人可以说一种有效的方法来做到这一点。到目前为止,这是我尝试读取和打印每个键值对的尝试:


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);


        }}


波斯汪
浏览 116回答 2
2回答

蝴蝶刀刀

forEach从JDK-8开始,可以通过以下方式完成此操作:list.forEach(map -> {&nbsp; &nbsp; &nbsp; String fieldName = map.get("fieldName");&nbsp; &nbsp; &nbsp; if("birthDate".equals(fieldName)) map.put("accessibilityType", "READ");});
随时随地看视频慕课网APP

相关分类

Java
我要回答