7

For Example,

List<Product> productsList = new ArrayList<Product>();  
productsList.add(new Product(1,"HP Laptop",25000f));  
productsList.add(new Product(2,"Dell Laptop",30000f));  
productsList.add(new Product(3,"Lenevo Laptop",28000f));  
productsList.add(new Product(4,"Sony Laptop",28000f));  
productsList.add(new Product(5,"Apple Laptop",90000f));  

Float totalPrice = productsList.stream()  
                               .map(product->product.price)  
                               .reduce(0.0f,(sum, price)->sum+price);   
System.out.println(totalPrice); 

Here which is the functional interface, (sum, price)->sum+price is referring to?

Holger
  • 285,553
  • 42
  • 434
  • 765
Rence Abishek
  • 373
  • 1
  • 4
  • 16

2 Answers2

5

Look at the Stream Javadoc:

T reduce(T identity, BinaryOperator<T> accumulator)

(sum, price)->sum+price implements a BinaryOperator<Float> in your example.

This functional interface has a method that accepts two arguments of the same type (Float in your case) and returns a result of that same type.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Eran
  • 387,369
  • 54
  • 702
  • 768
5

Because you mentioned "functional interface method": reduce is actually calling apply of BiFunction (from which BinaryOperator extends).

Roland
  • 22,259
  • 4
  • 57
  • 84