Map to create new value from int array in Java

Java 8Object Oriented ProgrammingProgramming

Let’s say the following is our int array elements:

10, 50, 100, 200, 250, 300, 400, 500

Here, we are mapping and creating a new value by incrementing each int element with 1:

Arrays.stream(new int[] {10, 50, 100, 200, 250, 300, 400, 500}).map(val -> val + 1)

Now find the average:

Arrays.stream(new int[] {10, 50, 100, 200, 250, 300, 400, 500}).map(val -> val + 1).average()

The following is an example to Map and create new value from int array:

Example

import java.util.Arrays;
public class Demo {
   public static void main(String[] args) throws Exception {
      Arrays.stream(new int[] {10, 50, 100, 200, 250, 300, 400, 500}).map(val -> val + 1).average()
         .ifPresent(System.out::println);
   }
}

Output

227.25
raja
Published on 02-May-2019 12:28:56
Advertisements