-2

I have two classes: Country and City.

Country has the following attributes: code, name, capital, population, Continent and a list of type City.

City has code, name and population attributes.

I am trying to find the most populated city of each continent

I got the highest populated city of each country but how can I filter this by Continent which is a String, or can I re filter the list I got?

the highestPopulation get the highest populated city in each country and the continent list get the highest populated continent is there a way to mix those 2 to find the most populated city of each continent



    public static void main(String[] args) {

        List<Country> countries=new ArrayList<Country>();


        
        
        System.out.println();
        List<City> highestPopulation = countries.stream().map(x -> x.cities.stream().max(Comparator.comparingInt(City::getPopulation)).orElse(null)).toList();
        
        for(City c: highestPopulation) {
        System.out.println(c.code+" "+c.name+" "+c.population);
        }
        
        System.out.println();
        Map<String, Optional<Country>> continent= countries.stream().collect(Collectors.groupingBy(Country::getContinent,Collectors.maxBy(Comparator.comparingInt(Country::getPopulation))));
    
        for(Entry<String, Optional<Country>> c: con.entrySet()) {
            System.out.println(c.getKey()+" "+c.getValue().get().name);
            }


Al1.Java
  • 11
  • 1
  • Does this answer your question? [How to sum a list of integers with java streams?](https://stackoverflow.com/questions/30125296/how-to-sum-a-list-of-integers-with-java-streams) – mohammedkhan May 31 '22 at 11:02
  • @mohammedkhan not really – Al1.Java May 31 '22 at 11:05
  • @mohammedkhan my problem is that I don't know how to filter by Continent name – Al1.Java May 31 '22 at 11:06
  • Your problem is that you're asking "can I?" and waiting one hour to see your question getting closed instead of trying it yourself and finding out in one minute. You're using simple documented operations, if something is unclear to you, Google it. You're not the first person in the world to filter a list, so don't make it such a huge problem. If you intend to get multiple results from your stream operation, you'll need a grouping collector. – Kayaman May 31 '22 at 11:14
  • Please edit your question to remove everything not absolutely required to describe your problem. For example, the `name` of a `Country` is irrelevant and can and should be erased. Please also remove all code not relevant, bringing the amount down to maybe only a few lines of code. See [MCVE](https://stackoverflow.com/help/minimal-reproducible-example) – Bohemian May 31 '22 at 11:19
  • @Bohemian who said the name is irrelevant , and I posted the classes so people don't ask to post them later – Al1.Java May 31 '22 at 11:21
  • Without seeing your `Country` class it's hard to tell where the continent name is meant to come from. If it's an attribute on the class itself you can use `Stream` to filter by that continent: https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#filter-java.util.function.Predicate- – mohammedkhan May 31 '22 at 13:11
  • @mohammedkhan want me to add the classes ? – Al1.Java May 31 '22 at 13:14
  • @mohammedkhan I don't think you are understanding what I am asking, Continent name is an attribute inside country and each country has a List of Cities, I want to use the GroupingBY and at the stream the List cities inside each country – Al1.Java May 31 '22 at 13:17
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community May 31 '22 at 19:01

0 Answers0