I have two classes: Country
and City
.
Country has the following attributes: Countrycode
, Countryname
, capital
, population
, Continent
and a list of type City
.
City has countrycode
, name
and population
attributes.
I am trying to find the highest populated city of each Country.
I want to use the 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;
}
}
CodePudding user response:
As you want to do something for each country, you need to loop on the coutries, then apply the logic
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
Note : java variable naming convention is lowerCamelCase
class Country {
int code;
String name;
String capital;
int population;
String continent;
List<City> cities;
}
CodePudding user response:
I am trying to find the highest populated city of each Country.
That's how you can create a list of the most populated cities in 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);
Output:
City{CountryCode=1, name='Tokyo', population=100000}
City{CountryCode=2, name='NYC', population=4000000}
Important notes:
Providing
NoSuchElementException
an argument oforElseThrow()
doesn't make sense because parameterless version of this method throwNoSuchElementException
is a case of empty optional.Adhere to the Java Naming Conventions. Names of variables and methods should be written in so-called camel-case (mixed case) and always start with a lower case letter:
cities
,getCities()
,countryCode
,population
, etc.Write your code against interfaces not against concrete implementations, use
List
instead ofArrayList
. See What does it mean to "program to an interface"?Use access modifiers to encapsulate class-members withing the class. In order to be able to change a state of a particular field you need to introduce a method, avoid accessing the field directly from outside the class (don't access the field directly - it's not a good practice in Java):
private List<City> cities = new ArrayList<City>();
public void addCity(City city) {
cities.add(city);
}