我有这段代码可以获取应用程序中每天的用户数量。
List<UserApplication> usersOfCurrentMonth = userApplicationRepository.getUsersOfCurrentMonth();
Map<Object, Long> dailyUserActivity = usersOfCurrentMonth.stream().collect(Collectors.groupingBy(a-> ((UserApplication)a).getCreationDate(), Collectors.counting()));
查询
@Query(value = "select * from user_application a where year(a.creationDate) = year(current_date) and month(a.creationDate) = month(current_date)", nativeQuery = true)
List<UserApplication> getUsersOfCurrentMonth();
效果很好
2020-03-30T14:21:09.003+0000: 1
但是我错过了这个月剩下的日子。有没有办法将该月的所有其他日期添加 0?
例如
2020-03-01T14:21:09.003+0000: 0
2020-03-02T14:21:09.003+0000: 0
....
2020-03-30T14:21:09.003+0000: 1
最佳答案
您可以确定当月的天数,然后对所有天进行循环,使用 computeIfAbsent 将结果设置为零.
关于java - 将默认值添加到列表/ map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60932709/