简体   繁体   中英

Filter array list of objects by specific value of key

I have an array list like this and i would like to get the value of the key "Total" for the Product Name - Leisure 1 but not sure how to search or iterate through an array list of objects in Java spring boot

 "priceList": [
      {
        "Total": "10",
        "Stamp Duty": "10",
        "Main Policy": "0",
        "Product Name": "Leisure 1"
      },
      {
        "Total": "10",
        "Stamp Duty": "10",
        "Main Policy": "0",
        "Product Name": "Leisure 2"
      },
      {
        "Total": "10",
        "Stamp Duty": "10",
        "Main Policy": "0",
        "Product Name": "Leisure 3"
      },
      {
        "Total": "10",
        "Stamp Duty": "10",
        "Main Policy": "0",
        "Product Name": "Work 1"
      },
      {
        "Total": "10",
        "Stamp Duty": "10",
        "Main Policy": "0",
        "Product Name": "Work 2"
      }
    ]

i tried doing this. I created a function

public static <K, V> Stream<K> keys(Map<K, V> map, V value) {
        return map
          .entrySet()
          .stream()
          .filter(entry -> value.equals(entry.getValue()))
          .map(Map.Entry::getKey);
    }

Then i tried to cast my array list to a map and tried to filter through the list to find the exact object but to no avail

priceList.stream().map(x -> x.get("Total").toString()).filter(s -> s.get("Product Name") == planHeader).collect(Collectors.toList());

How can i be able to loop through or search through an array of objects and filter by a specific value?

Any help appreciated

The problem is with s.get("Product Name") == planHeader

Shouldn't you be comparing this with equals()?

You can use equals method to compare strings that are not == .
This method checks the actual contents of the string and the == operator checks whether the reference to the object is the same.

priceList.stream()
      .map(x -> x.get("Total").toString())
      .filter(s -> s.get("Product Name").equals(planHeader)) // here
      .collect(Collectors.toList());

If it's an arrayList, just iterate over it and for each object do the getter to get the total.

for (Item i : priceList) { 
    system.out.println(i.getTotal());
}

I guess from your post, that you use a data structure like this: List<Map<String, String>> priceList

You really should be using a custom class for you prices. If you want to go with lists and maps, the following few lines might help.

        List<Map<String, String>> priceList = readPrices();
        // stream the list
        String total = priceList.stream()
                // filter for the price's Product Name
                .filter(price -> "Leisure 1".equals(price.get("Product Name")))
                // find the first one
                .findFirst()
                // extract the Total from the found price
                .map(price -> price.get("Total"))
                // return null if no matching price found
                .orElse(null);

It's OK to access a map with a string key. The map uses hashCode and equals internally. Nothing to worry about here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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