Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
public class PopulationCounter {
static Map<String, Map<String, Long>> population;
public static void main(String[] args) {
Scanner scanner = new Scanner(;
population = new LinkedHashMap<>();
String[] data;
while (!(data = scanner.nextLine().split("\\|"))[0].equals("report")) {
String country = data[1];
String city = data[0];
long populationCount = Long.parseLong(data[2]);
if (!population.containsKey(country)) {
population.put(country, new LinkedHashMap<>());
population.get(country).put(city, populationCount);
.sorted((country1, country2) ->, Sum(country2.getValue().values())))
.forEachOrdered(item -> {
System.out.printf("%s (total population: %d)\n", item.getKey(), Sum(item.getValue().values()));
.sorted((city1, city2) -> -city1.getValue().compareTo(city2.getValue()))
.forEachOrdered(city -> System.out.printf("=>%s: %d\n", city.getKey(), city.getValue()));
private static long Sum(Collection<Long> values) {
long total = 0;
for (Long value : values) {
total+= value;
return total;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment