Print ArrayList using Lambda Expression in Java 8
This article will help you to understand Print ArrayList using Lambda Expression in Java 8.
Java Program of Print ArrayList using Lambda Expression in Java 8
0 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 |
package com.codenuclear.lambda; import java.util.Arrays; import java.util.List; public class LambdaExample1 { public static void main(String[] args) { List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); // Conventional way of printing arraylist for (int number : numbers) { System.out.print(number); } // Lambda Expression to print arraylist - Way 1 numbers.forEach((Integer value) -> System.out.print(value)); // Lambda Expression to print arraylist - Way 2 numbers.forEach(value -> System.out.print(value)); // Lambda Expression (method reference) to print arraylist - Way 3 numbers.forEach(System.out::print); } } |
Output:
Explanation
Let’s understand each line of program
Line 12: Straightforward way to print arraylist in java
The lambda expression is made of two parts the one on the left of the arrow symbol (->) listing its parameters and the one on the right containing its body.
Line 17: In this case the compiler automatically figures out that the lambda expression has the same signature of the only non implemented method of the Consumer interface (that for this reason is called a functional interface) and treat the first as it was an instance of the second, even if the generated bytecode could potentially be different.
Line 20: The declaration of the types of the lambda expression arguments can be, in the biggest part of cases, inferred by the compiler and then omitted.
Line 23: We can rewrite statement even more concisely using a method reference, another feature introduced in Java 8. More in details in Java 8 it is possible to reference both a static and an instance a method using the new :: operator as in.
I have 2 scenarios for SOP, how will you do this in Lamda, some lights please. THANKS
Scenario#1: Index starting from 0 and jumping the next index ie. i=i+2
for (int i = 0; i < listSize; i = i + 2) {
System.out.println(numbers.get(i));
}
Scenario#2: Index starting from 1 and jumping the next index ie., i=i+2
for(int i=1;i<listSize;i=i+2) {
System.out.println(numbers.get(i));
}
Thanks I was able to do it
Thread evenThread8 = new Thread(() -> {
List evenList = ReadList8Refctor.getIntegerList().stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
evenList.stream().forEach((value) -> {System.out.println(Thread.currentThread().getName()+” – “+value);});
}, “EVEN”);
Thread oddThread8 = new Thread(() -> {
List oddList = ReadList8Refctor.getIntegerList().stream().filter(i -> i % 2 != 0).collect(Collectors.toList());
oddList.stream().forEach((value) -> {System.out.println(Thread.currentThread().getName()+” – “+value);});
}, “ODD”);
evenThread8.start();
oddThread8.start();
Hello Navin,
Thanks for showing such enthusiasm. You have right solution with you. Just curious to know why you are using Threads.
You can print ODD and EVEN using below lines.
numbers.stream().filter(i -> i % 2 == 0).forEach(System.out::print);
numbers.stream().filter(i -> i % 2 != 0).forEach(System.out::print);
Thanks again for comments. Keep visiting codeNuclear.