Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 12111

Collections.max with custom Comparator on list

$
0
0

I have an array list of Location objects that I want to find the max and min value of. However, when calling collections.max(listN, PopulationComparator) it gives me the minimum value whereas for collection.min() it returns the max value. The problem is resolved once changing the order to be descending, but I don't understand why this would even matter.

static class PopulationComparator implements Comparator <Location>{    public int compare(Location a, Location b){       // ascending order        if(a.getPop() > b.getPop()){             return 1;        }else if(a.getPop() < b.getPop()){            return -1;        }else{            return 0;        }    }}public static void main(String[] args){Location maxArea = Collections.max(listN, new PopulationComparator());}

Viewing all articles
Browse latest Browse all 12111

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>