2022-01-28

how to collect from stream using multiple conditions

I'm trying to sort a list of Message object, this entity contain multiple attributs but only 4 are useful for us in this case :

  • Integer : Ordre
  • String : idOras
  • Date : sentDate
  • Integer : OrdreCalcule (a concatination of Ordre and sentDate "YYYYmmDDhhMMss" )

if this case, the conditions of selection are the following :

  • if two Messages have the same Ordre :
    • if they have the same idOras -> collect the newest one (newest sentDate) and remove the others
    • if they have different idOras -> collect both of them sorted by sentDate ASC
  • if two Messages have different Ordre :
    • collect both of them sorted by Ordre

for now I'm using this stream :

orasBatchConfiguration.setSortedZbusOrasList(messageList.stream()
            .collect(Collectors.groupingBy(Message::getIdOras,
                    Collectors.maxBy(Comparator.comparing(Message::getOrdreCalcule))))
            .values()
            .stream()
            .map(Optional::get)
            .sorted(Comparator.comparing(Message::getOrdreCalcule))
            .collect(Collectors.toList()));


from Recent Questions - Stack Overflow https://ift.tt/3G9n8Ea
https://ift.tt/eA8V8J

No comments:

Post a Comment