I have a combination of querying a database with jooq and post processing the result with the streams. However I feel that my code is not very readable and not concise enough. How can I improve my code in ways of better expressing my intent.
sql
.select(field("USER_NAME", String.class))
.from(table("CWD_USER"))
.fetch()
.stream()
.map(f -> f.getValue(field("USER_NAME", String.class)))
.collect(Collectors.groupingBy(s -> StringUtils.split(s, "-")[0], Collectors.counting()))
.entrySet().stream()
.sorted(new java.util.Comparator<Entry<String, Long>>() {
@Override
public int compare(Entry<String, Long> o1,
Entry<String, Long> o2) {
return o2.getValue().compareTo(o1.getValue());
}
})
.forEach(e -> System.out.println(String.format("%13s: %3d", e.getKey(), e.getValue())));
First I have problems with the multiple streaming. I first stream the result from jooq then I stream the collected map. Also the comparator seems way to prominent. Sure I could make a class out of it, but maybe there is another solution.