1

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

2
  • What is the input data type? Commented May 10, 2019 at 11:24
  • @RavindraRanwala if referring to planHeader it is a string. If referring to priceList is is an Map<String, Object> Commented May 16, 2019 at 7:37

4 Answers 4

1

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

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.