I want to take a set of objects (ObjectInstance in this case), and I want to group them by one property, and have the resulting lists be sorted on another.
Set<ObjectInstance> beans = server.queryMBeans(null, null);
Map<String, List<String>> beansByDomain = beans.stream()
.collect(groupingBy( (ObjectInstance oi) -> oi.getObjectName().getDomain(),
mapping((ObjectInstance oi) -> oi.getObjectName().getCanonicalKeyPropertyListString(),
toList() )));
The above expression creates the correct data structure: a Map where the keys are the domains of the ObjectInstance objects, and the values are Lists of the property lists. What I want is to now sort the Lists, to make sure they are in alphabetical order. Is there some way to do this in the same expression?
One idea would be to add .sort() right after .stream(), but is that really guaranteed to work?