In this tutorial, we will see “How to convert an Array to an ArrayList in Java 8?” array to arraylist java 8
Convert an Array to ArrayList in Java 8 with example...!!! Share on X
Table of Contents
- Example 1. Convert Primitive Int Array to ArrayList – Method – 1
- Example 2. Convert Primitive Int Array to ArrayList – Method – 2
- Example 3. Convert Integer Array to ArrayList
- stream to array java
- Example 4. Convert String Array to ArrayList – Method – 1
- stream to array java
- Example 5. Convert String Array to ArrayList – Method – 2
- stream to array java
Example 1. Convert Primitive Int Array to ArrayList – Method – 1
/**
* Convert Primitive Int Array to ArrayList - Method - 1
* @author Deepak Verma
*/
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Convert_Primitive_Int_Array_to_ArrayList_using_Java8_Method_1 {
public static void main(String[] args) {
int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
List<Integer> listOfIntegers = Arrays.stream(intArray).boxed().collect(Collectors.toList());
System.out.println("List: " + listOfIntegers);
}
}
Output:
List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example 2. Convert Primitive Int Array to ArrayList – Method – 2
/**
* Convert Primitive Int Array to ArrayList - Method - 2
* @author Deepak Verma
*/
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Convert_Primitive_Int_Array_to_ArrayList_using_Java8_Method_2 {
public static void main(String[] args) {
int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
List<Integer> listOfIntegers = IntStream.of(intArray).boxed().collect(Collectors.toList());
System.out.println("List: " + listOfIntegers);
}
}
Output:
List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example 3. Convert Integer Array to ArrayList
/**
* Convert Integer Array to ArrayList
* @author Deepak Verma
*/
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Convert_Integer_Array_to_ArrayList_using_Java8 {
public static void main(String[] args) {
Integer integerArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
List<Integer> listOfIntegers = Arrays.stream(integerArray).collect(Collectors.toList());
System.out.println("List: " + listOfIntegers);
}
}
Output:
List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
stream to array java
Example 4. Convert String Array to ArrayList – Method – 1
/**
* Convert String Array to ArrayList - Method - 1
* @author Deepak Verma
*/
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Convert_String_Array_to_ArrayList_using_Java8_Method_1 {
public static void main(String[] args) {
String stringArray[] = {"Tom", "Bill", "Alex", "Henry", "Adam"};
List<String> listOfStrings = Stream.of(stringArray).collect(Collectors.toList());
System.out.println("List: "+listOfStrings);
}
}
Output:
List: [Tom, Bill, Alex, Henry, Adam]
stream to array java
array to arraylist java 8 array to arraylist java 8 array to arraylist java 8 array to arraylist java 8
Example 5. Convert String Array to ArrayList – Method – 2
/**
* Convert String Array to ArrayList - Method - 2
* @author Deepak Verma
*/
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Convert_String_Array_to_ArrayList_using_Java8_Method_2 {
public static void main(String[] args) {
String stringArray[] = {"Tom", "Bill", "Alex", "Henry", "Adam"};
ArrayList<String> listOfStrings = Stream.of(stringArray).collect(Collectors.toCollection(ArrayList::new));
System.out.println("List: "+listOfStrings);
}
}
Output:
List: [Tom, Bill, Alex, Henry, Adam]
stream to array java
array to arraylist java 8
Convert an Array to ArrayList in Java 8 with example...!!! Share on X
Do you like this Post? – then check my other helpful posts:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: