Java Lambda Expression Constructor Reference for arrays

Introduction

To create a constructor reference for an array:

type[]::new 

Here, type specifies the type of object being created.

import java.util.Arrays;
import java.util.function.IntFunction;

public class Main {

  public static void main(String args[]) {
    IntFunction<int[]> arrayCreator1 = size -> new int[size];
    int[] empIds1 = arrayCreator1.apply(5); // Creates an int array of five elements
    System.out.println(Arrays.toString(empIds1));
  }//from w w  w  .  j  a  v a  2s  .c  o  m
}



PreviousNext

Related