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



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.

3 Thoughts on “Print ArrayList using Lambda Expression in Java 8”

  • 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) {
    Scenario#2: Index starting from 1 and jumping the next index ie., i=i+2
    for(int i=1;i<listSize;i=i+2) {

    • Thanks I was able to do it
      Thread evenThread8 = new Thread(() -> {
      List evenList = ReadList8Refctor.getIntegerList().stream().filter(i -> i % 2 == 0).collect(Collectors.toList()); -> {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()); -> {System.out.println(Thread.currentThread().getName()+” – “+value);});
      }, “ODD”);


      • 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. -> i % 2 == 0).forEach(System.out::print); -> i % 2 != 0).forEach(System.out::print);

        Thanks again for comments. Keep visiting codeNuclear.

Leave a Reply

Your email address will not be published. Required fields are marked *