SHARE
TWEET

Untitled

a guest Apr 20th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Arrays;
  2. import java.util.List;
  3. import java.util.function.Consumer;
  4.  
  5. public class Main {
  6.  
  7.     private static LamdaExampleClass lamdaExampleClass = new LamdaExampleClass();
  8.  
  9.     /*
  10.     Foreach Method: default void forEach(Consumer<? super T> action)
  11.     foreach requires Consumer<String> in our case as Parameter and Consumer String is an Interface which everyone can implement
  12.      */
  13.  
  14.     public static void main(String[] args) {
  15.         List<String> list = Arrays.asList("elem1","elem2");
  16.         list.forEach(s -> System.out.println(s));//same as:
  17.         list.forEach(s -> lamdaFu(s));//same as:
  18.         list.forEach(Main::lamdaFu);//same as:
  19.         list.forEach(lamdaExampleClass);
  20.     }
  21.  
  22.     private static void lamdaFu(String s) {
  23.         System.out.println(s);
  24.     }
  25.  
  26.     static class LamdaExampleClass implements Consumer<String>{
  27.  
  28.         @Override
  29.         public void accept(String s) {
  30.             System.out.println(s);
  31.         }
  32.     }
  33. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top