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

后端 未结 4 1656
遇见更好的自我
遇见更好的自我 2020-12-11 04:28

I have two classes that are structured like this:

public class Company {
     private List person;
     ...
     public List getP         


        
相关标签:
4条回答
  • 2020-12-11 05:00

    Obligatory Guava way.

    import java.util.List;
    import java.util.stream.Collectors;
    
    import com.google.common.math.Quantiles;
    
    ...
    
    List<Person> people = company.getPerson();
    List<Double> ages = people.stream().map(Person::getAge).collect(Collectors.toList());
    double median = Quantiles.median().compute(ages);
    

    Though as of Guava 28.1 Quantiles is still annotated as @Beta.

    0 讨论(0)
  • 2020-12-11 05:01

    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.

    0 讨论(0)
  • 2020-12-11 05:18

    Here's a simplified version of @Holger's answer that also works for IntStream and LongStream, and avoids NoSuchElementException in the case of an empty stream:

    int size = someList.size();
    
    //replace 'XXX' with 'Int', 'Long', or 'Double' as desired
    return someList.stream().mapToXXX(...).sorted()
        .skip((size-1)/2).limit(2-size%2).average().orElse(Double.NaN);
    

    This will return NaN if the list is empty rather than throwing NoSuchElementException. If you'd prefer to throw a NoSuchElementException instead, simply replace .orElse(Double.NaN) with .getAsDouble().

    0 讨论(0)
  • 2020-12-11 05:18

    You can use lambda expression reduction to get the median of list:

    Integer median = Person
       .stream()
       .map(Person::getAge)
       .filter(n -> n.length()/2);
    
    0 讨论(0)
提交回复
热议问题