How do I group a list inside an object by two of the object attributes?
I'm using Drools 7.9.0 and Java 8. I have a Result
class that is used as return to each matched Drools rule.
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@Data
@NoArgsConstructor
public class Result implements Serializable {
private Integer id;
private String name;
private List<Occurrences> occurrences = new ArrayList<>();
public void addOccurrence(Occurrences occurrence) {
this.occurrences.add(occurrence);
}
}
After Drools execution, I end up with a List<Result>
. Converted to JSON it looks like this.
CURRENT OUTCOME:
[
{
name: "Whatever"
id: 0001,
occurrences: [{
(...)
}]
},
{
name: "Whatever"
id: 0001,
occurrences: [{
(...)
}]
},
{
name: "Whatever"
id: 0002,
occurrences: [{
(...)
}]
},
{
name: "Other thing"
id: 0002,
occurrences: [{
(...)
}]
}
]
I need to group up my list of Result
s so the ocurrences
are grouped by id
and name
, like this.
EXPECTED OUTCOME:
[
{
name: "Whatever"
id: 0001,
occurrences: [{
(...)
}, {
(...)
}]
},
{
name: "Whatever"
id: 0002,
occurrences: [{
(...)
}]
},
{
name: "Other thing"
id: 0002,
occurrences: [{
(...)
}]
}
]
What would be the best way to implement this? I have two options:
List<Result>
is already structured the way I need. Maybe create a class with a addResult
method that checks the list for id
and name
and add the occurrence to the right entry. But this is not ideal, because it will increase complexity inside the rules.List<Result>
so it groups the occurrences
by id
and name
. But I have no idea how to do this in an optimized and simple way.What's the best way to do the 2nd option?
You may do it like so,
Map<String, List<Result>> resultsWithSameIdAndName = results.stream()
.collect(Collectors.groupingBy(r -> r.getId() + ":" + r.getName()));
List<Result> mergedResults = resultsWithSameIdAndName.entrySet().stream()
.map(e -> new Result(Integer.valueOf(e.getKey().split(":")[0]),
e.getKey().split(":")[1],
e.getValue().stream().flatMap(r -> r.getOccurrences().stream())
.collect(Collectors.toList())))
.collect(Collectors.toList());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With