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



我有两个类:CountryCity

Country有以下属性:Countrycode,Countryname,capital,population,Continent和类型为City的列表。

City具有countrycodenamepopulation属性。

我想找出每个国家人口最多的城市。

我想使用流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变量命名约定为lowerCamelCase

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

我想找出每个国家人口最多的城市。

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

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}

在线演示的链接

重要提示:

  • NoSuchElementException提供orElseThrow()的参数是没有意义的,因为该方法的无参数版本throwNoSuchElementException空可选的情况。

  • 遵守Java命名约定。变量和方法的名称应该用所谓的骆驼大小写(混合大小写),并且总是以小写字母开头:cities,getCities(),countryCode,population,等等。

  • 针对接口编写代码,而不是针对具体实现,使用List而不是ArrayList。参见"编程到接口"是什么意思?

  • 使用访问修饰符将类成员封装在类中。为了能够更改特定字段的状态,您需要引入一个方法,避免直接从类外部访问该字段(不要直接访问该字段—这在Java中不是一个好的实践):

private List<City> cities = new ArrayList<City>();
public void addCity(City city) {
cities.add(city);
}

最新更新