java functional programming consumer function supplier code example

Example 1: 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);}

Example 2: 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