#498 Finding the Country with the Highest Population Density
From the source list of countries, return the country with the highest population-to-area ratio:
import java.util.*;
class Scratch {
public static void main(String[] args) {
List<Country> countries = Arrays.asList(new Country("country_1", 100, 5000),
new Country("country_2", 9000, 500000),
new Country("country_8", 6527, 6324687),
new Country("country_11", 872321, 765237),
new Country("country_9", 823743, 63543762),
new Country("country_3", 800, 40000));
Country c = getTheBiggestCountry(countries);
System.out.println(c);
}
public static Country getTheBiggestCountry(List<Country> countries) {
// return the country with the highest population-to-area ratio
return null;
}
static class Country {
public final String name;
public final double area;
public final long population;
public String getName() {
return name;
}
public double getArea() {
return area;
}
public long getPopulation() {
return population;
}
public Country(String name, double area, long population) {
this.name = name;
this.area = area;
this.population = population;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Country)) {
return false;
}
Country country = (Country) o;
return Double.compare(country.area, area) == 0 && population == country.population && name.equals(country.name);
}
@Override
public int hashCode() {
return Objects.hash(name, area, population);
}
@Override
public String toString() {
return "Country{" +
"name='" + name + '\'' +
", area=" + area +
", population=" + population +
'}';
}
}
}
Question review
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor