Created
February 2, 2018 06:07
-
-
Save RICH0423/f82730de0aeaa076b041443735a49f38 to your computer and use it in GitHub Desktop.
Program to Convert 'int' Array to 'List' of Integer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
public class Array2ListDemo { | |
public static void main(String[] args) { | |
/**** Converting a Primitive 'int' Array to List ****/ | |
int intArray[] = {1, 2, 3, 4, 5}; | |
List<Integer> integerList1 = Arrays.stream(intArray).boxed().collect(Collectors.toList()); | |
System.out.println("List #1: " + integerList1); | |
/**** 'IntStream.of' or 'Arrays.stream' Gives The Same Output ****/ | |
List<Integer> integerList2 = IntStream.of(intArray).boxed().collect(Collectors.toList()); | |
System.out.println("List #2: " + integerList2); | |
/**** Converting an 'Integer' Array to List ****/ | |
Integer integerArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; | |
List<Integer> integerList3 = Arrays.stream(integerArray).collect(Collectors.toList()); | |
System.out.println("List #3: " + integerList3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment