functional interface examples in java 8 function supplier consumer

Example 1: what is a method example in java

public int addNum(int num1, int num2) {
  total = num1 + num2;
  System.out.println("Total: " + total);
}

Example 2: creating the functional interface in java

package com.concretepage;

@FunctionalInterface
public interface Calculator {
   long calculate(long num1, long num2);
}

Example 3: java 8 function supplier consumer

@Testpublic void whenNamesPresentUseBothConsumer(){    
  List<String> cities = Arrays.asList("Sydney", "Dhaka", "New York", "London");    
  Consumer<List<String>> upperCaseConsumer = list -> {
    for(int i=0; i< list.size(); i++){            
      list.set(i, list.get(i).toUpperCase());        
    }    
  };    
  Consumer<List<String>> printConsumer = list -> list.stream().forEach(System.out::println);    
  upperCaseConsumer.andThen(printConsumer).accept(cities);}

Tags:

Java Example