Home > OS >  Find a value by key in List of Maps in Java
Find a value by key in List of Maps in Java

Time:04-29

I have a list of Maps in following manner:

List<Map<String,String>> input = 
[{AddressField=AddressUsageType, AddressValue=PRINCIPAL},  {AddressField=StreetNumber, AddressValue=2020}, {AddressField=StreetName, AddressValue=Some street}]

I would like to get the value of AddressValue from value of AddressField. For eg.,

I want to get the value "PRINCIPAL" from the key value "AddressUsageType"

I have tried using filters and many other MAP functions but couldn't end up with a proper solution.

This is my code snippet that gets the value of 1st key-value pair:

DataTable table;
List<Map<String,String>> input= table.asMaps(String.class, String.class);

    String AddressField = input.get(0).get("AddressField");
    String AddressValue = input.get(0).get("AddressValue");
    System.out.println("AddressField "  AddressField);
    System.out.println("AddressValue "  AddressValue);

Here is the output of above snippet:

AddressField AddressUsageType
AddressValue PRINCIPAL

CodePudding user response:

Your usage of Maps is a bit odd here because your actual key "AddressUsageType" is a value inside the map and every map is just one pair of key and value stored behind static keys. If you can not change that, you could go with something like this:

String key = "AddressUsageType";
String result = "";

for (Map<String,String> map : input)
{
    if (map.containsValue(key))
    {
        result = map.get("AddressValue");
        break;
    }
}

System.out.println(key   " is "   result);

CodePudding user response:

I think this is what you're looking for:

Map<String, String> map = data.stream()
        .filter(m -> m.values().contains("AddressUsageType"))
        .findFirst()
        .orElse(null);

if (map != null) {
    System.out.println("AddressField "   map.get("AddressField"));
    System.out.println("AddressValue "   map.get("AddressValue"));
}

Here's also a test main

public class Test {
    public static void main(String[] args) {
        List<Map<String, String>> data = new ArrayList<>();

        Map<String, String> map1 = new HashMap<>();
        map1.put("AddressField", "AddressUsageType");
        map1.put("AddressValue", "PRINCIPAL");
        data.add(map1);

        Map<String, String> map2 = new HashMap<>();
        map2.put("AddressField", "StreetNumber");
        map2.put("AddressValue", "2020");
        data.add(map2);

        Map<String, String> map3 = new HashMap<>();
        map3.put("AddressField", "StreetName");
        map3.put("AddressValue", "Some street");
        data.add(map3);

        Map<String, String> map = data.stream()
                .filter(m -> m.values().contains("AddressUsageType"))
                .findFirst()
                .orElse(null);

        if (map != null) {
            System.out.println("AddressField "   map.get("AddressField"));
            System.out.println("AddressValue "   map.get("AddressValue"));
        }
    }
}

Output

enter image description here

CodePudding user response:

Filter the input to retain only maps which contain AddressField=AddressUsageType. Then extract the AddressValue entry using the .map function and gather them in a result list.

public static void main(String[] args) {
    List<Map<String, String>> input = new ArrayList<>();
    Map<String, String> map = new HashMap<>();
    map.put("AddressField", "AddressUsageType");
    map.put("AddressValue", "PRINCIPAL");
    input.add(map);

    map = new HashMap<>();
    map.put("AddressField", "StreetNumber");
    map.put("AddressValue", "2020");
    input.add(map);
    map = new HashMap<>();
    map.put("AddressField", "StreetName");
    map.put("AddressValue", "Some street");
    input.add(map);

    map = new HashMap<>();
    map.put("AddressField", "AddressUsageType");
    map.put("AddressValue", "NOT_PRINCIPAL");
    input.add(map);
    
    List<String> collect = input.stream().filter(e -> e.get("AddressField").equals("AddressUsageType"))
            .map(e -> e.get("AddressValue")).collect(Collectors.toList());
    System.out.println(input);
    System.out.println(collect);
}

The output is

[{AddressField=AddressUsageType, AddressValue=PRINCIPAL}, {AddressField=StreetNumber, AddressValue=2020}, {AddressField=StreetName, AddressValue=Some street}, {AddressField=AddressUsageType, AddressValue=NOT_PRINCIPAL}]

[PRINCIPAL, NOT_PRINCIPAL]

  • Related