简体   繁体   中英

Group items in a list of objects in Java

I'm doing a web app with Angular + Java (Oracle -RDBMS). In a page I display the data contained in the Dto that I send back to browser in the Responsive (after it is converted into json, obviously). It works, but this Dto contains a List of Objects which contains:

| FOOD | CUSTOMER | COUNT
  Apple     X         3
  Apple     y         1
  Apple     z         5
  Milk      j         2
  Milk      p         1

This is the process that I do:

    List<FoodsDto> foods = new ArrayList<FoodsDto>();
    // I call the query to retrieve the list and I add it ordering for 'foods'...
    // Then I set it on the result 
    result.setFoods(developmentResult);
    // And i send the response on browser...

Before the 'setFoods' I'd like to group the list for foods. The result should be a new array containing:

| FOOD | CUSTOMER | COUNT
  Apple     X         3
  Apple     y         1
  Apple     z         5
  Milk      j         2
  Milk      p         1

  Apple  9
  Milk   3

'9' and '3' is the sum of the count, so the total. In turn these lines must contain a subarray with all information. So:

[Apple 9] --
           |--> Apple x 3
           |--> Apple y 1
           |--> Apple z 5

[Milk  3] --
           |--> Milk j 2
           |--> Milk p 1

How can I do to ' break ' the list and group it?

If you do not want to create a separate DTO, you can simple iterate over the list of FoodsDto and use another Map<String, Integer> for grouping, as shown below.

Map<String, Integer> foodGroup = new HashMap<>();
 for(FoodsDto foodsDto : foods) {
    if(foodGroup.containsKey(foodsDto.getFood())){
       foodGroup.put(foodsDto.getFood(), (foodGroup.get(foodsDto.getFood()) + foodsDto.getCount())); 
    } else {
       foodGroup.put(foodsDto.getFood(), foodsDto.getCount());
    }
}

And then send foodGroup as well in your response. On front end (in Javascript / AngularJs), you need to map foodGroup and foods , using food name as key to display it the way you want.

'9' and '3' is the som of the count, so the total. In turn these lines must contain a subarray with all information.

You could use a map to group the FoodsDto items by food:

    Map<FoodsDto, List<FoodsDto>> map = new HashMap<>();        

    for(FoodsDto o : developmentResult){
        // using the FoodsDto as the key
        if (map.get(o) != null) {
            map.get(o).add(o);
        } else {
            List<FoodsDto> foodList = new ArrayList<FoodsDto>();
            foodList.add(o);
            map.put(o, foodList);
        }
    }

    for (Map.Entry<FoodsDto, List<FoodsDto>> entry : map.entrySet()) {
        List<FoodsDto> list = entry.getValue();
        System.out.println(String.format("%s: %d", entry.getKey(), list.size()));

        for(FoodsDto f : list){
            System.out.println(f);
        }
    }

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