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?
}
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.