Sum especific property of same item in list of lists using java 8 streams : Given a List of lists and using java 8 Streams API, I need to sum all quantities of each same item in different lists grouped by your id.

For the example, I have the following code:

    public class Streams {
        
        static class PurchaseItemCancellation {
            private Integer id;
            private BigDecimal quantity;
    
            // constructor
            // getters and setters
        }
        
        static class PurchaseCancellation {
            private Integer id;
            private List<PurchaseItemCancellation> purchaseItemsCancellations;
            
            // constructor
            // getters and setters    
        }
            
        public static void main (String ... args) {
    
        PurchaseItemCancellation item1 = new PurchaseItemCancellation(1, new BigDecimal("10.00"));
        PurchaseItemCancellation item2 = new PurchaseItemCancellation(2, new BigDecimal("20.00"));
        PurchaseItemCancellation item3 = new PurchaseItemCancellation(3, new BigDecimal("30.00"));
        PurchaseItemCancellation item4 = new PurchaseItemCancellation(4, new BigDecimal("40.00"));
    
        PurchaseCancellation purchaseCancellation1 = new PurchaseCancellation(1, Arrays.asList(item1, item2));
        PurchaseCancellation purchaseCancellation2 = new PurchaseCancellation(2, Arrays.asList(item3, item4));
        PurchaseCancellation purchaseCancellation3 = new PurchaseCancellation(3, Arrays.asList(item4, item1));
    
        List<PurchaseCancellation> cancellations = Arrays.asList(purchaseCancellation1, purchaseCancellation2, purchaseCancellation3);
    
        final Comparator<PurchaseItemCancellation> byID = (p1, p2) -> Integer.compare(p1.getId(), p2.getId());
    
        // List<PurchaseItemCancellation> itemsCancellations = cancellations.stream() ....
    
        }
    }
    

The `List<PurchaseCancellation> cancellations` has lists of cancellations on which each cancellation has a list of `PurchaseItemCancellation`;

So I want to sum all quantities of same item for each list of `PurchaseItemCancellation` contained on each cancellation.

The result must be a `List<PurchaseItemCancellation>`:

item1: id: 1, quantity: 20.00

item2: id: 2, quantity: 20.00

item3: id: 3, quantity: 30.00

item4: id: 4, quantity: 80.00

A executable code could be found in:

[http://www.tutorialspoint.com/compile_java8_online.php?PID=0Bw_CjBb95KQMREE4TEJxa080NFE][1]

**P.S.: This site has two annoying bugs.**

**First bug:** When you click the link is opened a file "HelloWorld.java".
Close this file and open the correct file :"Streams.java" on the left sidebar.
The execute button is on the top of editor.

**Second bug:** if the file is modified you must generate a new sharing link by clicking in
"Share code" button on the top of editor.
Sorry about this site. I don't know a better online java 8 code editor.

Any help will be apreciated.


  [1]: http://www.tutorialspoint.com/compile_java8_online.php?PID=0Bw_CjBb95KQMREE4TEJxa080NFE