title: Java stream. Sum two fields in a stream of objects
body: <p>I have something like this:</p>
  
  <pre><code>Integer totalIncome = carDealer.getBrands().stream().mapToInt(brand -&gt; brand.getManufacturer().getIncome()).sum();
  Integer totalOutcome = carDealer.getBrands().stream().mapToInt(brand -&gt; brand.getManufacturer().getOutcome()).sum();
  </code></pre>
  
  <p>How could I write that in one stream ? to collect f.e. <code>Pair&lt;Integer, Integer&gt;</code> with <code>totalIncome</code> and <code>totalOutcome</code> ?</p>
  
  <p><strong>EDITED</strong>:</p>
  
  <p>Thank you guys for your comments, answers, and involvment. I would have a question about different approach to that problem using streams. What do you think about that:</p>
  
  <pre><code>final IncomeAndOutcome incomeAndOutcome = carDealer.getBrands()
                      .stream()
                      .map(Brand::getManufacturer)
                      .map(IncomeAndOutcome::of)
                      .reduce(IncomeAndOutcome.ZERO, IncomeAndOutcome::sum);
  
  static class IncomeAndOutcome {
  
      private static final IncomeAndOutcome ZERO = of(0, 0);
  
      @Getter
      private final int income;
  
      @Getter
      private final int outcome;
  
      public static IncomeAndOutcome of(final int income, final int outcome) {
          return new IncomeAndOutcome(income, outcome);
      }
  
      public static IncomeAndOutcome of(final Manufacturer manufacturer) {
          return new IncomeAndOutcome(manufacturer.getIncome(), manufacturer.getOutcome());
      }
  
      IncomeAndOutcome(final int income, final int outcome) {
          this.income = income;
          this.outcome = outcome;
      }
  
      IncomeAndOutcome sum(final IncomeAndOutcome incomeAndOutcome) {
          return of(this.income + incomeAndOutcome.getIncome(), this.outcome + incomeAndOutcome.getOutcome());
      }
  }
  </code></pre>