Lambda Expression in Java 8
In this example , Refactoring from external to internal iterator by simple examples with printing values.
In order to write java code to print array elements. in previous versions of java.
lot of code need to write for an iterations. For example :
We can may simplify the above code by using forEach Loop:
In this example , Refactoring from external to internal iterator by simple examples with printing values.
In order to write java code to print array elements. in previous versions of java.
lot of code need to write for an iterations. For example :
1 2 3 4 5 6 7 8 9 10 11 12 | import java.util.Arrays; import java.util.List; public class TestLamda { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); for (int i = 0; i < numbers.size(); i++) { System.out.println("Numbers:" + numbers.get(i)); } } } |
We can may simplify the above code by using forEach Loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import java.util.Arrays; import java.util.List; public class TestLamda { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); /*for (int i = 0; i < numbers.size(); i++) { System.out.println("Numbers:" + numbers.get(i)); }*/ for(int j : numbers){ System.out.println(j); } } } |
The above 2 Ways are we called in Java External Iterator.
Java Divided iterators based on who controls the iterations process?
A fundamental task is deciding which party controls the iteration. the iterator or the client that uses the iterator.
External Iterator:
When the client controls the iteration, the iterator is called an external iterator (Example: C++ and Java).
Internal Iterator :
when the iterator controls it, the iterator is an internal iterator (Example : Lisp and functional languages)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import java.util.Arrays; import java.util.List; import java.util.function.Consumer; public class TestLamda { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); /* * for (int i = 0; i < numbers.size(); i++) { * System.out.println("Numbers:" + numbers.get(i)); } */ /* * for(int j : numbers){ System.out.println(j); } */ numbers.forEach(new Consumer<Integer>() { @Override public void accept(Integer value) { System.out.println(value); } }); } } |
The above one is better way to do the iteration at run time polymorphism.
But we are created unanimous inner class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | import java.util.Arrays; import java.util.List; import java.util.function.Consumer; /** * @author nareshn * */ public class TestLamda { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); /* * for (int i = 0; i < numbers.size(); i++) { * System.out.println("Numbers:" + numbers.get(i)); } */ /* * for(int j : numbers){ System.out.println(j); } */ /* * numbers.forEach(new Consumer<Integer>() { * * @Override public void accept(Integer value) { * System.out.println(value); } }); */ numbers.forEach((Integer value) -> System.out.println(value)); } } |
This one is called a simple Lambda function in it.
A Function should contains 4 parts: 1.Body , 2.Name, 3.Parameter List, 4.Return Type.
Here rather than accepting single abstract method , interface , now forEach method is more than willing to accept Lambda expression
In Java 1.0 , Thread Class rather than sending a new Runnable , now we can send a Lambda expression. it is very good move.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.util.Arrays; import java.util.List; import java.util.function.Consumer; /** * @author nareshn * */ public class TestLamda { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); /* * for (int i = 0; i < numbers.size(); i++) { * System.out.println("Numbers:" + numbers.get(i)); } */ /* * for(int j : numbers){ System.out.println(j); } */ /* * numbers.forEach(new Consumer<Integer>() { * * @Override public void accept(Integer value) { * System.out.println(value); } }); */ /*numbers.forEach((Integer value) -> System.out.println(value));*/ //no need to mention return type numbers.forEach(value -> System.out.println(value)); } } |
Here we no need to mention return type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import java.util.Arrays; import java.util.List; import java.util.function.Consumer; /** * @author nareshn * */ public class TestLamda { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); /* * for (int i = 0; i < numbers.size(); i++) { * System.out.println("Numbers:" + numbers.get(i)); } */ /* * for(int j : numbers){ System.out.println(j); } */ /* * numbers.forEach(new Consumer<Integer>() { * * @Override public void accept(Integer value) { * System.out.println(value); } }); */ /* numbers.forEach((Integer value) -> System.out.println(value)); */ // no need to mention return type /* numbers.forEach(value -> System.out.println(value)); */ // perform some operations multiply, addition,substact or more for given // array values. numbers.forEach(value -> System.out.println(value * 2)); } } |
Here we can not only a value, some mathematical operations to the array elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import java.util.Arrays; import java.util.List; import java.util.function.Consumer; /** * @author nareshn * */ public class TestLamda { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); /* * for (int i = 0; i < numbers.size(); i++) { * System.out.println("Numbers:" + numbers.get(i)); } */ /* * for(int j : numbers){ System.out.println(j); } */ /* * numbers.forEach(new Consumer<Integer>() { * * @Override public void accept(Integer value) { * System.out.println(value); } }); */ /* numbers.forEach((Integer value) -> System.out.println(value)); */ // no need to mention return type /* numbers.forEach(value -> System.out.println(value)); */ // perform some operations multiply, addition,substact or more // for given // array values. /* numbers.forEach(value -> System.out.println(value * 2)); */ // if we print direct value numbers.forEach(System.out::println); } } |