How to convert List of a POJO to Map<String,List> in Java Stream?

shuvidora :

I want to convert a List of Java POJO into a Map in a non-static method with Java 8 Stream API.

My line chart needs a list of date String values for axis x and a list of numeric values for axis y. It's a typical Map format. But my database return a List of POJOs for me. I feel dislike looping without the help of Java 8 Stream API. I've tried the method in this [ask}(Java 8 List of Objects to Map<String, List> of values). However, I am faced with two problems.First, My POJO MoreCustomDTO contains Integer besides String. Second, When I try to use method reference IDEA complains about non-static method cannot be referenced from a static context.

POJO:

@Data
MoreCustomDTO {
    private String date;
    private Integer money;
}

DAO query method:

public List<MoreCustomDTO > getMoreCustomCount(@Param("format") String format, @Param("startTime") String startTime, @Param("endTime") String endTime);

Solution before Java 8:

List<MoreCustomCountDTO> customList = customDao.getMoreCustomCount(SqlUtils.DATE_TYPE_FORMAT[Integer.valueOf(type)],startTime,endTime);
Map<String, List> map = new HashMap();
List<String> dateList = new ArrayList<>();
List<Integer> moneyList = new ArrayList<>();
for (MoreCustomCountDTO data : customList) {
    dates.add(data.getDate());
    dailyAmounts.add(data.getMoney());
}
map.put("date", dateList);
map.put("money", moneyList);

Failure code segment:

Map<String,List> map =
                    customList.stream()
                            .flatMap(element -> {
                                Map<String,String> um = new HashMap<>();
                                um.put("date",element.getDate());
                                um.put("money",element.getMoney());
                                return um.entrySet().stream();
                            })
                            .collect(Collectors.groupingBy(Map.Entry::getKey,
                                    Collectors.mapping(Map.Entry::getValue,
                                            Collectors.toList())));

I get a List from my database. And it is a Object array in JSON foramt.

response:

{
  "rows": [
    {
      "date": "2019-09-01",
      "money": 0.00
    },
    {
      "date": "2019-09-02",
      "money": 0.00
    }
  ]
}

But I want a one(key)-to-many(values) Map format.

response:

{
  "map": {
    "date": [
      "2019-09-01",
      "2019-09-02"
    ],
    "money": [
      0.00,
      0.00
    ]
  }
}
Luis Iñesta :

Honestly, I think your initial solution was ok. Sometimes forcing a solution to be implemented using the most fancy features of the language ends up with less clear code, which is always a bad thing.

Having said that, I think what you intended to do is something like:

Map<Object, Object> map = Stream.of(
    new SimpleEntry<>(
      "date",
      customList.stream().map(MoreCustomDTO::getDate).collect(Collectors.toList())
    ),
    new SimpleEntry<>(
      "money",
      customList.stream().map(MoreCustomDTO::getMoney).collect(Collectors.toList())
    )
  ).collect(Collectors.toMap(SimpleEntry::getKey, SimpleEntry::getValue));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=164375&siteId=1