Suppose I have the following domain class:
public class OrderRow {
private Long orderId;
private String action;
private LocalDateTime timestamp;
// getters, constructor, etc.
}
I have a following data set of OrderRows :
OrderId Action Timestamp
3 Pay money 2015-05-27 12:48:47.000
3 Select Item 2015-05-27 12:44:47.000
1 Generate Payment 2015-05-27 12:55:47.000
2 Pay money 2015-05-27 12:48:47.000
2 Select Item 2015-05-27 12:44:47.000
2 Deliver 2015-05-27 12:55:47.000
1 Generate Invoice 2015-05-27 12:48:47.000
1 Create PO 2015-05-27 12:44:47.000
3 Deliver 2015-05-27 12:55:47.000
What I want to obtain the following Map from the sample data shown above:
[3] -> ["Select Item", "Pay money", "Deliver"]
[1] -> ["Create PO", "Generate Invoice", "Generate Payment"]
[2] -> ["Select Item", "Pay money", "Deliver"]
By performing below operations :
- I want to groupBy
orderId. - Sort actions by
timestamp. - Create a
Set(as there can be duplicates) ofactions.
I am trying to do this in a single groupingBy operation as performing separate sorting, mapping operations take a lot of time if data set is huge.
I've tried to do the following:
orderRows.stream()
.collect(Collectors.groupingBy(OrderRow::getOrderId,
Collectors.mapping(Function.identity(),
Collectors.toCollection(
() -> new TreeSet<>(Comparator.comparing(e -> e.timestamp))
))));
But then I get output as Map<String, Set<OrderRow>>
where as I need the result of type Map<String, Set<String>>.
Would be really grateful if someone can show me at least a direction to go.
Note that is a critical operation and should be done in few milliseconds, hence performance is important.