Finding the median value from a List of objects using Java 8

000000000000000000000 picture 000000000000000000000 · Apr 27, 2017 · Viewed 9.8k times · Source

I have two classes that are structured like this:

public class Company {
     private List<Person> person;
     ...
     public List<Person> getPerson() {
          return person;
     }
     ...
}

public class Person {
     private Double age;
     ...
     public Double getAge() {
          return age;
     }
     ...
}

Basically the Company class has a List of Person objects, and each Person object can get an Age value.

If I get the List of the Person objects, is there a good way to use Java 8 to find the median Age value among all the Person objects (Stream doesn't support median but is there anything else)?

Double medianAge;
if(!company.getPerson().isEmpty) {
     medianAge = company.getPerson() //How to do this in Java 8?
}

Answer

Holger picture Holger · Apr 28, 2017

You may use

List<Person> list = company.getPerson();
DoubleStream sortedAges = list.stream().mapToDouble(Person::getAge).sorted();
double median = list.size()%2 == 0?
    sortedAges.skip(list.size()/2-1).limit(2).average().getAsDouble():        
    sortedAges.skip(list.size()/2).findFirst().getAsDouble();

The advantage of this approach is that it doesn’t modify the list and hence also doesn’t rely on its mutability. However, it’s not necessarily the simplest solution.

If you have the option of modifying the list, you can use

List<Person> list = company.getPerson();
list.sort(Comparator.comparingDouble(Person::getAge));
double median = list.get(list.size()/2).getAge();
if(list.size()%2 == 0) median = (median + list.get(list.size()/2-1).getAge()) / 2;

instead.