Example of mapToInt in Java 8

UPDATED: 08 October 2015

IntStream mapToInt(ToIntFunction<? super T> mapper);
Returns an IntStream consisting of the results of applying the given function to the elements of this stream.

Source code (Item.java)
public class Item {
    private String Name;
    private int Quantity;

    public String getName() {
        return Name;
    }

    public void setName(String Name) {
        this.Name = Name;
    }

    public int getQuantity() {
        return Quantity;
    }

    public void setQuantity(int Quantity) {
        this.Quantity = Quantity;
    }
}

Source code
import java.util.ArrayList;
import java.util.List;

public class MapToIntExample {
    public static void main(String[] args) {
        /* Create an object of `Item` and set values */
        Item itemSamsungGalaxy6 = new Item();
        itemSamsungGalaxy6.setName("Samsung Galaxy 6");
        itemSamsungGalaxy6.setQuantity(20);
        
        Item itemNexus = new Item();
        itemNexus.setName("Nexus 5");
        itemNexus.setQuantity(12);
        
        /**
         * - Create `List` of `Item`
         * - Add `itemSamsungGalaxy6` and `itemNexus` to List.
         */
        List<Item> listItems = new ArrayList<>();
        listItems.add(itemSamsungGalaxy6);
        listItems.add(itemNexus);
        
        /**
         * JavaDoc: `mapToInt` method
         * Returns an IntStream consisting of the results of applying the given function to the elements of this stream.
         * 
         * Explanation: `.mapToInt(Item::getQuantity)` / `.mapToInt(item -> item.getQuantity())`
         * In this operation we are calling method `getQuantity()` on each object of `Item` in List.
         * That returns the `IntStream` of value(Quantity) of all object in List.
         * And we are doing summation of all quantity
         */
        int totalQuantity = listItems.stream()
                                     .mapToInt(Item::getQuantity)
                                     .sum();
        
        /* Print the total quantity */
        System.out.println("Total Quantity: " + totalQuantity);
    }
}

Explanation: .mapToInt(Item::getQuantity) / .mapToInt(item -> item.getQuantity())
In this operation we are calling method getQuantity() on each object of Item in List. That returns the IntStream of value(Quantity) of all object in List. And we are doing summation of all quantity


Output
Total Quantity: 32
Other References:
Example of mapToDouble in Java 8

0 comments :