/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.stream.Collectors;
import java.util.function.Function;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
{
final Collection<Record> records = new ArrayList<>();
records.add(new Record("11111", "Orlando"));
records.add(new Record("11111", "Orlando"));
records.add(new Record("11111", "Orlando"));
records.add(new Record("11111", "Los Angeles"));
records.add(new Record("11111", "Los Angeles"));
records.add(new Record("11111", "San Francisco"));
records.add(new Record("22222", "Cincinatti"));
records.add(new Record("22222", "Cleveland"));
records.add(new Record("22222", "Cleveland"));
records.add(new Record("22222", "Columbus"));
records.add(new Record("22222", "Columbus"));
records.add(new Record("22222", "Columbus"));
final Map
<String, List
<Record
>> recordsByZip
= records.
stream() .collect(Collectors.groupingBy(Record::getZip));
recordsByZip.forEach((zip, entries) -> {
final Map
<String, Long
> frequencyMap
= entries.
stream() .map(Record::getCity)
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.
out.
println("Frequency map for zip " + zip
); frequencyMap.
forEach((city, frequency
) -> System.
out.
println(city
+ ": " + frequency
)); });
}
}
class Record {
this.zip = zip;
this.city = city;
}
return zip;
}
public void setZip
(String zip
) { this.zip = zip;
}
return city;
}
public void setCity
(String city
) { this.city = city;
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CmltcG9ydCBqYXZhLnV0aWwuc3RyZWFtLkNvbGxlY3RvcnM7CmltcG9ydCBqYXZhLnV0aWwuZnVuY3Rpb24uRnVuY3Rpb247CgovKiBOYW1lIG9mIHRoZSBjbGFzcyBoYXMgdG8gYmUgIk1haW4iIG9ubHkgaWYgdGhlIGNsYXNzIGlzIHB1YmxpYy4gKi8KY2xhc3MgSWRlb25lCnsKCXB1YmxpYyBzdGF0aWMgdm9pZCB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