-1

I have one List of totally 13 HashMaps in my input. Each HashMap has only 2 Keys "fieldName" and "accessibilityType" and corresponding values:

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

        }}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 1
    Why would you use List of HashMap rather than a much more simple list of POJO? I have to wonder if your question is in fact an [XY Problem](http://xyproblem.info/) question where your whole approach may be incorrect. – Hovercraft Full Of Eels Jul 14 '18 at 23:32
  • Hi, I don't know what a list of POJO is. I am new to this so just playing with HashMaps and wanted to learn something new – Ron Samuel Jul 14 '18 at 23:36
  • Have you tried [`e.setValue`](https://docs.oracle.com/javase/8/docs/api/java/util/Map.Entry.html#setValue-V-)? – MadProgrammer Jul 14 '18 at 23:36
  • 1
    POJO: plain Old Java Object, create a class of FieldAccessibility with two fields, a fileName String and an editable boolean. Use a JSON library to parse the file and help create a `List` filled with objects of this type. – Hovercraft Full Of Eels Jul 14 '18 at 23:39
  • @MadProgrammer if (key=="accessibilityType") e.setValue("READ_VALUE"); I tried this but it changes the values in every HashMap – Ron Samuel Jul 14 '18 at 23:45
  • @HovercraftFullOfEels, thank you for the suggestion but in this situation I just want to deal with the basics first – Ron Samuel Jul 14 '18 at 23:49
  • @RonSamuel [How do I compare strings in Java?](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – MadProgrammer Jul 14 '18 at 23:51
  • Do you mean key.equals("accessibilityType") ?? still doesn't work – Ron Samuel Jul 14 '18 at 23:55
  • this is **not** anywhere near *the basics* of Java or any other OO language. –  Jul 15 '18 at 00:03
  • 1
    @feelingunwelcome how is this a dupe of the aforementioned? – Ousmane D. Jul 15 '18 at 00:05
  • right there in the comments *@MadProgrammer if (key=="accessibilityType") e.setValue("READ_VALUE");* –  Jul 15 '18 at 00:06
  • 1
    @feelingunwelcome in the comments..., not in the post itself. also, how is this a duplicate of "Im trying to loop over an array of objects to return the indexes where there is a match" ? – Ousmane D. Jul 15 '18 at 00:07
  • 1
    I don't know why you aren't just using a single Map containing `{{"firstName", "editable"}, ...}`. The extra level of indirection adds nothing. – user207421 Jul 15 '18 at 00:08

2 Answers2

1

it can be done as follows using forEach as of JDK-8:

list.forEach(map -> {
      String fieldName = map.get("fieldName");
      if("birthDate".equals(fieldName)) map.put("accessibilityType", "READ");
});
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
1

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. Otherwise leave the existing value as it is. Finally collect all the new maps into a result container.

Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63