使用 Stream API 从国家列表中查找每个国家中人口最多的城市

Find the City with highest population in each Country from a List of Countries using Stream API

我有两个 类:CountryCity

国家/地区具有以下属性:CountrycodeCountrynamecapitalpopulationContinent 和类型列表 City .

城市有 countrycodenamepopulation 属性。

我试图找到每个国家/地区中人口最多的城市。

我想使用 Stream API。

import java.util.ArrayList;
import java.util.Comparator;
import java.util.NoSuchElementException;

public class Main13 {

    public static void main(String[] args) {
        
        ArrayList<Country> Countries=new ArrayList<Country>();
        
        //Countrycode,Countryname,capital,population,Continent
        
        Country coun1=new Country(1,"Japan","Tokyo",4000000,"Asia");
        Country coun2=new Country(2,"USA","DC",400000000,"America");
        
        
        City c1=new City(1,"Tokyo",100000);
        City c2=new City(1,"Osaka",10000);
        City c3=new City(1,"Nagoya",20000);
        
        City n1=new City(2,"NYC",4000000);
        City n2=new City(2,"LA",1000000);
        
        
        coun1.Cities.add(c1);
        coun1.Cities.add(c2);
        coun1.Cities.add(c3);
        
        coun2.Cities.add(n1);
        coun2.Cities.add(n2);
        
        Countries.add(coun1);
        Countries.add(coun2);
        
        Country Max2=Countries.stream().max(Comparator.comparingInt(Country::getpop)).orElseThrow(NoSuchElementException::new);
        
        System.out.println(Max2.Countryname);
    }

}

////////////////////////////////////////////
import java.util.ArrayList;
import java.util.List;

public class Country {
    int countrycode;
    String Countryname; 
    String Capital; 
    int Population;
    String Continent; 
    ArrayList<City> Cities=new ArrayList<City>();

    public Country(int code,String n,String c,int p,String con) {
        countrycode=code;
        Countryname=n; 
        Capital=c; 
        Population=p;
        Continent=con;
    }

    public int getpop() {
        return Population;
    }

}
////////////////////////

public class City {

    int CountryCode;
    String name;
    int Population;
    
    
    
    public City(int code,String n,int pop) {
        CountryCode=code;
        name=n;
        Population=pop;
    }
}

因为你想为每个国家做点什么,你需要循环国家,然后应用逻辑

for (Country c : countries) {
    City max2 = c.cities.stream().max(Comparator.comparingInt(City::getPopulation))
            .orElseThrow(NoSuchElementException::new);
    System.out.println(c.getName() + " " + max2.getName() + " " + max2.getPopulation());
}
Japan Tokyo 100000
USA NYC 4000000

注意:java变量命名约定是小驼峰式

class Country {
    int code;
    String name;
    String capital;
    int population;
    String continent;
    List<City> cities;
}

I am trying to find the highest populated city of each Country.

这就是创建每个国家/地区人口最多城市列表的方法:

List<City> largestCities = countries.stream()
    .map(country -> country.getCities().stream().max(Comparator.comparingInt(City::getPopulation)))
    .map(Optional::orElseThrow)
    .collect(Collectors.toList());
    
largestCities.forEach(System.out::println);

输出:

City{CountryCode=1, name='Tokyo', population=100000}
City{CountryCode=2, name='NYC', population=4000000}

A link to the Online Demo

重要提示:

  • 提供 NoSuchElementException 参数 orElseThrow() 没有意义,因为此方法的 parameterless version throw NoSuchElementException空可选.

  • 坚持Java Naming Conventions。变量名和方法名应该写成 so-called camel-case大小写混合)并且总是以小写开头字母:citiesgetCities()countryCodepopulation

  • 针对接口而非具体实现编写代码,使用 List 而不是 ArrayList。参见 What does it mean to "program to an interface"?

  • 使用访问修饰符将 class-members 封装到 class 中。为了能够更改特定字段的状态,您需要引入一种方法,避免直接从 class 外部访问该字段( 不要直接访问该字段 - 它不是Java):

    中的良好做法
private List<City> cities = new ArrayList<City>();

public void addCity(City city) {
    cities.add(city);
}