Search code examples
javajava-8sumjava-streamnested-lists

get List from with maximum and minimum sum from (nested List) List of List using Java 8


I was checking these question but it's not answer my question

How to get minimum and maximum value from List of Objects using Java 8
Find maximum, minimum, sum and average of a list in Java 8

My problem I have nested List, and I want to obtain one List.

I have this class:

public class Competitor {
  private final int type;
  private final String name;
  private final int power;

  public Competitor(int type, String name, int power) {
    this.type = type;
    this.name = name;
    this.power = power;
  }

  public int getType() {
    return type;
  }

  public String getName() {
    return name;
  }

  public int getPower() {
    return power;
  }

  @Override
  public String toString() {
    return "Competitor{" + "type=" + type + ", name=" + name + ", power=" + power + "} ";
  }

}

Now I have a List<List<Competitor>> like:

List<List<Competitor>> anotherListOfListCompetitor = new ArrayList<>();
anotherListOfListCompetitor.add(new ArrayList<>(
    Arrays.asList(new Competitor(1, "Cat 00", 93), new Competitor(2, "Dog 18", 40), new Competitor(3, "Pig 90", 90)))); //93 + 40 + 90 = 223

anotherListOfListCompetitor.add(new ArrayList<>(
    Arrays.asList(new Competitor(1, "Cat 23", 20), new Competitor(2, "Dog 30", 68), new Competitor(3, "Pig 78", 32)))); //20 + 68 + 32 = 120

anotherListOfListCompetitor.add(new ArrayList<>(
    Arrays.asList(new Competitor(1, "Cat 10", 11), new Competitor(4, "Cow 99", 90)))); //11 + 90 = 101

Now, I want to obtain the List<Competitor> with minimum sum of power Property and other List<Competitor> with maximum sum.

I know of reduce...

List<Competitor> someListCompetitor = //populate the list

int sumPower = someListCompetitor.stream()
    .map(competitor -> competitor.getPower())
    .reduce(0, (a, b) -> a + b);

But is it possible for List<List<Competitor>> obtains the minListCompetitor with minimal sumPower and anotherListCompetitor with maximal sumPower?

List<Competitor> minListCompetitor = anotherListOfListCompetitor
    .stream()... // which sum power is 101


List<Competitor> maxListCompetitor = anotherListOfListCompetitor
    .stream()... // which sum power is 223

How obtain these lists?


Solution

  • You can use this:

    List<Competitor> minListCompetitor = anotherListOfListCompetitor.stream()
            .min(Comparator.comparingInt(l -> l.stream().mapToInt(Competitor::getPower).sum()))
            .orElse(Collections.emptyList());
    
    List<Competitor> maxListCompetitor = anotherListOfListCompetitor.stream()
            .max(Comparator.comparingInt(l -> l.stream().mapToInt(Competitor::getPower).sum()))
            .orElse(Collections.emptyList());
    

    This returns the .min() / .max() of the sum from the lists. It uses a simplified version of the code you provided to calculate the sum.

    If you want to throw an exception if no maximum is found you can use .orElseThrow().