Hello dear colleagues.
As an exercise for Java streams, I've written simple program, which scrapes links to references from a news portal. Basically, I wanted to find out, which portals are the most referenced.
Right now, I ended up with the following piece of code:
final Map<String, List<URL>> hostToURLs = Analyzer.mapByHost(ReferencesStore.read());
// E.g. bbc.com -> [ https://www.bbc.com/article1, https://www.bbc.com/article2, etc ]
// The following creates LinkedHashMap sorted by the number of URLs
final LinkedHashMap<String, List<URL>> sortedHostToURLs = hostToURLs
.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(Comparator.comparingInt(List::size)))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (m, n) -> { m.addAll(n); return m; }, LinkedHashMap::new));
I'm interested, if the the ugly merge lambda
(m,n)-> {m.addAll(n); return m;}
in the collect method can be replaced with some method reference from standard library. I wasn't able to find anything useful in the documentation, nor was I able to ask google the right question to find out if such merge function exists. Thank you in advance.
Map.Entry
\$\endgroup\$