0

I would like to make a list of maps being grouped by and summed up with a certain key.

List<Tag> tagList = new ArrayList<>();

# Tag
HashMap<String, Object> tagMap = new HashMap<>();

## feeding data example
tagMap.put("category", "A");
tagMap.put("name", "Apple");
tagMap.put("price", 10);

tagList.add(tagMap)

tagList = 
    [
      {
        "category": "A",
        "name": "apple",
        "price": 10
      },
      {
        "category": "B",
        "name": "banana",
        "price": 20
      },
      {
        "category": "C",
        "name": "candy",
        "price": 30
      },
      {
        "category": "A",
        "name": "apple",
        "price": 20
      },
      {
        "category": "B",
        "name": "banana",
        "price": 10
      },
      {
        "category": "C",
        "name": "candy",
        "price": 20
      }
    ]

I ve already grouped tagList by category

Map<String, List<Tag>> tagGrouped =
       tagList.stream()
       .collect(Collectors.groupingBy(tag -> tag.getCategory()));

Here is the example

Map<String, List<Tag>> tagGrouped =

        {
          "A": [
            {
              "category": "A",
              "name": "apple",
              "price": 10
            },
            {
              "category": "A",
              "name": "apple",
              "price": 20
            },
            {
              "category": "A",
              "name": "apple",
              "price": 30
            }
          ],
          "B": [
            {
              "category": "B",
              "name": "banana",
              "price": 10
            },
            {
              "category": "B",
              "name": "banana",
              "price": 20
            }
          ],
          "C": [
            {
              "category": "C",
              "name": "candy",
              "price": 10
            },
            {
              "category": "C",
              "name": "candy",
              "price": 10
            },
            {
              "category": "C",
              "name": "candy",
              "price": 20
            }
          ]
    }

This is the sample output that I want to get finally

tagGroupedList = 
    [
      {
        "category": "A",
        "name": "apple",
        "price": 60
      },
      {
        "category": "B",
        "name": "banana",
        "price": 30
      },
      {
        "category": "C",
        "name": "candy",
        "price": 40
      }
    ]

I got stuck at this point..

List<Tag> tagGroupedList =
                tagGrouped.stream()
                .collect(Collectors.groupingBy(Tag::getCategory))
                .entrySet()
                .stream()
                .flatMap(e -> e.getValue().stream())
                .collect(Collectors.summarizingInt(Tag::getPrice));
  • your input says category `B` but name as `candy`. Is this right ? – Pramod Jul 07 '21 at 04:35
  • Sorry it was a typo! it should be `C`. `A` for `Apple`, `B` for `Banana` and `C` for `Candy` but name is not really matter it could be anything in this example. – steamworks99 Jul 07 '21 at 04:49

2 Answers2

0

It is best to read the official documentation and refresh your knowledge of maps. https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html

0

This is similar to your previous question

Collection<Tag> tagGroupedList  = tagGrouped.values().stream().flatMap(tags -> tags.stream()).collect(Collectors.toMap(tag -> tag.getCategory(), Function.identity(), (tag1, tag2) -> {
        tag1.setPrice(tag1.getPrice() + tag2.getPrice());
        return tag1;
    })).values();
   //tagGroupedList will have the data what you need
Pramod
  • 787
  • 4
  • 19
  • Thanks for your code!! i will just check and give you an update!! – steamworks99 Jul 07 '21 at 04:53
  • You are the master of JAVA.. i actually tried to use your previous answer on this questions as well. but I couldn't really get the answer by myself.. i think I really need to learn a lot.. Thank you so much!! I really appreciate it – steamworks99 Jul 07 '21 at 04:56
  • Would you mind having a look at my first question and give me an advice to solve this question as well? i ve got 2 answers from another 2 genius guys but I got stuck from somewhere when I applied the codes into my project. I will just update the original one with my current situation https://stackoverflow.com/questions/68250988/find-the-missing-elements-of-a-list-not-in-the-values-of-a-hashmap – steamworks99 Jul 07 '21 at 05:07