I have an Topic:
@Entity
public class Topic {
@Id
private int id;
private LocalDate date;
private String name;
private int points;
@JoinColumn(name = "user_id", nullable = false)
private User user;
}
I'm getting list of topics in given date by spring data jpa method:
List topics = topicRepository.findByDateBetween(begin, end); which on output has e.g:
Topic(id=1, date="2018-01-01", name="Java examples", User(...), 12)
Topic(id=2, date="2018-02-02", name="Java examples", User(...), 34)
Topic(id=3, date="2018-02-02", name="Java examples", User(...), 56)
Topic(id=4, date="2018-03-03", name="Java examples", User(...), 78)
Topic(id=5, date="2018-03-03", name="Java examples", User(...), 90)
What I try to achive is to filter my result output as (if date && User is the same add points)
Topic(id=1, date="2018-01-01", name="Java examples", User(...), 12)
Topic(id=2, date="2018-02-02", name="Java examples", User(...), 90)
Topic(id=4, date="2018-03-03", name="Java examples", User(...), 168)
My actual solution returns map with date as key and summed points as value, but I need to have more data given in output like User or name.
return topics.stream()
.collect(Collectors.groupingBy(Topic::getDate,
Collectors.summingInt(Topic::getPoints)));
Maybe there is another way to instead of map return created dto for that case? e.g.
@Data
public class ResultDto {
private LocalDate date;
private String name;
private int points;
private User user;
}