The main reason why this question is difficult to answer, is because we don't know how many possible values there are for FirstEntity
to hold. For this reason, we must use a Map<FirstEntity, List<Objecto>>
where, for every FirstEntity
, we store a List<Objecto>
that share the FirstEntity
attribute.
For this to compile, you must create a getter in your Objecto
class for FirstEntity
:
public FirstEntity getFirstEntity() {
return first;
}
Then, the List<Objecto>
can be streamed and collected into a Map<FirstEntity, List<Objecto>>
:
Map<FirstEntity, List<Objecto>> map = thirds.stream().collect(Collectors.groupingBy(Objecto::getFirstEntity));
For this to work, you must override Object#equals
and Object#hashCode
within FirstEntity
.