Get object with max frequency from Java 8 stream
<p>I have an object with <code>city</code> and <code>zip</code> fields, let's call it <code>Record</code>.</p>

<pre><code>public class Record() {
    private String zip;
    private String city;

    //getters and setters
}
</code></pre>

<p>Now, I have a collection of these objects, and I group them by <code>zip</code> using the following code:</p>

<pre><code>final Collection&lt;Record&gt; records; //populated collection of records
final Map&lt;String, List&lt;Record&gt;&gt; recordsByZip = records.stream()
    .collect(Collectors.groupingBy(Record::getZip));
</code></pre>

<p>So, now I have a map where the key is the <code>zip</code> and the value is a list of <code>Record</code> objects with that <code>zip</code>.</p>

<p>What I want to get now is the most common <code>city</code> for each <code>zip</code>.</p>

<pre><code>recordsByZip.forEach((zip, records) -&gt; {
    final String mostCommonCity = //get most common city for these records
});
</code></pre>

<p>I would like to do this with all stream operations. For example, I am able to get a map of the frequency for each <code>city</code> by doing this:</p>

<pre><code>recordsByZip.forEach((zip, entries) -&gt; {
    final Map&lt;String, Long&gt; frequencyMap = entries.stream()
        .map(GisSectorFileRecord::getCity)
        .filter(StringUtils::isNotBlank)
        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
});
</code></pre>

<p>But I would like to be able to do a single-line stream operation that will just return the most frequent <code>city</code>.</p>

<p>Are there any Java 8 stream gurus out there that can work some magic on this?</p>

<p>Here is an <a href="https://ideone.com/IhL5te" rel="noreferrer">ideone sandbox</a> if you'd like to play around with it.</p>