Java 8: Lambda expression contains more than one statement / logic

I wrote a simple program to iterate through Listusing java 8 lambda.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class FirstLamdaExpression {

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        //Way 1 : old way
        list.forEach(new Consumer<Integer>() {

            @Override
            public void accept(Integer t) {
                System.out.print(t + " ");

            }
        });

        //Way 2
        System.out.println(" ");
        list.forEach((Integer t) -> System.out.print(t + " "));

        //Way 3
        System.out.println(" ");
        list.forEach((t) -> System.out.print(t + " "));

        //Way 4
        System.out.println(" ");
        list.forEach(System.out::print);

    }

}

In the program below, I have more than two pieces of logic to execute inside a lambda. The problem I am facing is how to update the 4th method, i.e. System.out::print?

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

public class SecondLamdaExpression {

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        //Way 1 : Old way
        list.forEach(new Consumer<Integer>() {

            @Override
            public void accept(Integer t) {
                System.out.print(t + "  Twice is : ");
                System.out.print(t*2 + "  , ");

            }
        });

        //Way 2
        System.out.println(" ");
        list.forEach((Integer t) -> 
             {System.out.print(t + "  Twice is : ");
                System.out.print(t*2 + "  , ");
             });

        //Way 3
        System.out.println(" ");
        list.forEach((t) ->  {System.out.print(t + "  Twice is : ");
        System.out.print(t*2 + "  , ");
         });

        //Way 4
        //System.out.println(" ");
        //list.forEach((t)-> System.out::print{(t + "  Twice is : ");});

    }

}
+4
source share
2 answers

It seems you are asking how to pass a t + " Twice is : " + t*2 + " , "reference to a method. You cannot pass explicit parameters to method references, and you cannot combine method references with lambda expressions.

Stream map t, t, forEach :

list.stream().map(t -> t + "  Twice is : " + t*2 + "  , ").forEach(System.out::print);
+5

- . , . , , - :

public class SecondLamdaExpression {
    public static void main(String[] args) {    
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

        // First three ways not repeated here

        // Way 4
        System.out.println(" ");
        list.forEach(SecondLamdaExpression::printTwice);
    }

    private static void printTwice(Integer t) {
        System.out.print(t + "  Twice is : ");
        System.out.print(t*2 + "  , ");
    }
}
+4

Source: https://habr.com/ru/post/1648925/


All Articles