There are serval ways in which we can convert Array into ArrayList in Java, let's take a look at a few examples.
Example 1: Arrays.asList()
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
public class ArrayToArrayListExample1 {
public static void main(String[] args) {
String[] arrayOfStrings = {"A", "B", "C", "D", "E"};
List<String> list = Arrays.asList(arrayOfStrings);
ArrayList<String> arrayList = new ArrayList<>(list);
System.out.println(arrayList);
}
}
Output:
Example 2: Using Loops
import java.util.ArrayList;
public class ArrayToArrayListExample2 {
public static void main(String[] args) {
String[] arrayOfStrings = {"A", "B", "C", "D", "E"};
ArrayList<String> arrayList = new ArrayList<>();
for (String element : arrayOfStrings) {
arrayList.add(element);
}
System.out.println(arrayList);
}
}
Example 3: Using Java 8 Stream API
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ArrayToArrayListExample3 {
public static void main(String[] args) {
String[] arrayOfStrings = {"A", "B", "C", "D", "E"};
List<String> list = Arrays.stream(arrayOfStrings)
.collect(Collectors.toList());
ArrayList<String> arrayList = new ArrayList<>(list);
System.out.println(arrayList);
}
}
Example 4: Collections.addAll()
import java.util.ArrayList;
import java.util.Collections;
public class ArrayToArrayListExample4 {
public static void main(String[] args) {
Integer[] arrayOfIntegers = {1, 2, 3, 4, 5};
ArrayList arrayList = new ArrayList<>();
Collections.addAll(arrayList, arrayOfIntegers);
System.out.println(arrayList);
}
}
Output:
Conclusion:
Use Arrays.asList() for quick, fixed-size list conversion; use a loop for element processing or large arrays; use Collections.addAll() for adding elements to an existing ArrayList; and use the Stream API for functional programming and additional processing in Java 8 and later.
You can download this article in various formats for your convenience. Choose from the options below:
Facing issues? Have Questions? Post them here! I am happy to answer!
- Java Jackson ObjectMapper Class with Examples
- How to add a Delay of a Few Seconds in Java Program
- Java SE JDBC Select Statement Example
- How to Word-Warp Console logs in IntelliJ
- Convert Java Array to ArrayList Code Example
- [Java] Error: Unmappable character for encoding UTF-8. Save could not be completed.
- Maven BUILD FAILURE: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin
- How to extract Java Jar/War/Ear files in Linux
- Java: Convert String to Binary
- Java Generics: Type parameter cannot be instantiated directly
- How to Create and Run a Command Line Application using Spring Boot
- How to Convert a Java Object into an Optional with Example
- [Hibernate] The method buildSessionFactory() from the type Configuration is deprecated
- How to Set JAVA_HOME PATH to Mac .zshrc profile file
- Formatting Double in Java [Examples]
- Java 8: Stream map with Code Examples
- Fix: SyntaxError: ( was never closed [Python]
- Java Split String by Spaces
- [fix] Java JDBC SQLSyntaxErrorException: Unknown database
- Fix: SpringFramework BeanDefinitionValidationException: Could not find an init method named
- Java Date Time API: LocalDateTime get(TemporalField field) examples
- How to create Date in yyyy-MM-dd format in Java
- Java: Create Temporary Directory and File and Delete when application terminates
- Fix RabbitMQ: AuthenticationFailureException: ACCESS_REFUSED
- Run Java Code Every Second
- How to Align Text using Notepad++ - NotepadPlusPlus
- Setting and Updating AWS CLI Configuration - AWS
- How to remove username from Mac Menu Bar? - MacOS
- The default username and password for RabbitMQ - HowTos
- Installing Visual Studio Code on Raspberry Pi - HowTos
- Python Program To Calculate Simple Interest (SimpleInterest.py) - Python
- How to count the files in a directory using Bash Command - Bash
- Java Program: Find Name of the Employee with Maximum Salary using Stream API - Java