Assume I have the following domain object:
public class MyObj {
private Long id;
private Long relationId;
private Long seq;
// getters
}
There is a list List<MyObj> list
. I want to create a Map by grouping the data by relationId
(*key) and sort values (value is a list of id
).
My code without sort values:
List<MyObj> list = getMyObjs();
// key: relationId, value: List<Long> ids (needs to be sorted)
Map<Long, List<Long>> map = list.stream()
.collect(Collectors.groupingBy(
MyObj::getRelationId,
Collectors.mapping(MyObj::getId, toList())
));
public class MyObjComparator{
public static Comparator<MyObj> compare() {
...
}
}
I have created compare method MyObjComparator::compare
, my question is how to sort this map's values in the above stream.