I have a two classes Formulary and Pieces. Formulary has to keep a list of Pieces, because it is a formulary of pieces and I need to keep track of which Pieces Objects are in a Formulary:
class Piece {
String id;
String name;
String info;
}
class Formulary{
String id;
TreeMap<String, Piece> pieces;
public Collection<Piece> getPieces() {return pecas.values();}
}
Formulary has a method getPieces() to return the list of pieces it has contained.
It happens that I need to join pieces that are in multiple formularies. This is what i did so far:
TreeMap<String, Formulary> forms;
List<Collection<Pieces>> piecesFromForms = forms.values().stream().map(Formulary::getPieces).collect(Collectors.toList())
The problem is that I was only capable to return a list<Collection<Pieces>>
, but need to join all the list of pieces in the forms and return a List<Pieces>
and I couldn't find a way to do this with collections.
I could do with for loops. I know, but I'm trying to get better with collections, as they work much faster.