Java 8 List to Arrays

Java 8 convert List to Arrays

  • List
List integerList = Arrays.asList(1, 2, 3, 4, 5);

 

  • OutPut type : Object[]
Object[] objArray = integerList.stream().toArray();
Arrays.stream(objArray).forEach(System.out::println);


1
2
3
4
5

 

  • OutPut type : Integer[]
Integer[] intArray = integerList.stream().toArray(Integer[]::new);
Arrays.stream(intArray).forEach(System.out::println);

1
2
3
4
5