Java Program to get the reverse of an Integer array with Lambda Expressions

Java 8Object Oriented ProgrammingProgramming

Let us first declare and initialize an Integer array:

Integer[] arr = {20, 50, 75, 100, 120, 150, 170, 200};

To find the reverse of the Integer array with Lambda, use sort and the Lamda:

Arrays.sort(arr, (Integer one, Integer two) -> { return (two- one); });

The following is an example to reverse an Integer array with Lambda Expressions:

Example

import java.util.Arrays;
public class Demo {
   public static void main(String[] args) {
      Integer[] arr = {20, 50, 75, 100, 120, 150, 170, 200};
      System.out.println("Integer Array elements...");
      for (int res : arr)
      {
         System.out.println(res);
      }
      Arrays.sort(arr, (Integer one, Integer two) -> { return (two- one); });
      System.out.println("Reverse of Array elements...");
      System.out.println(Arrays.toString(arr));
   }
}

Output

Integer Array elements...
20
50
75
100
120
150
170
200
Reverse of Array elements...
[200, 170, 150, 120, 100, 75, 50, 20]
raja
Published on 02-May-2019 10:45:25
Advertisements