📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
List
interface. This guide will cover different ways to convert an array to a list, including using the Arrays.asList
method, the ArrayList
constructor, and the Stream
API (Java 8 and later).Table of Contents
- Introduction
- Using
Arrays.asList
Method - Using
ArrayList
Constructor - Using
Stream
API - Conclusion
Introduction
In Java, arrays are fixed-size data structures, whereas lists are part of the Java Collections Framework and provide dynamic sizing and additional utility methods. Converting an array to a list allows you to use these additional functionalities.
Using Arrays.asList
Method
The Arrays.asList
method is the simplest way to convert an array to a list. However, the returned list is a fixed-size list backed by the original array, meaning you cannot add or remove elements.
Example
import java.util.Arrays; import java.util.List; public class ArrayToListExample { public static void main(String[] args) { String[] array = {"apple", "banana", "cherry"}; // Convert array to list List<String> list = Arrays.asList(array); System.out.println("Array: " + Arrays.toString(array)); System.out.println("List: " + list); } }
Explanation
- The
Arrays.asList
method converts the array to a list. - The resulting list is fixed-size and backed by the original array.
Output:
Array: [apple, banana, cherry] List: [apple, banana, cherry]
Using ArrayList
Constructor
To create a resizable list from an array, you can use the ArrayList
constructor that takes a collection as an argument. This approach creates a new ArrayList
that can be modified independently of the original array.
Example
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ArrayToListExample { public static void main(String[] args) { String[] array = {"apple", "banana", "cherry"}; // Convert array to list List<String> list = new ArrayList<>(Arrays.asList(array)); System.out.println("Array: " + Arrays.toString(array)); System.out.println("List: " + list); // Modify the list list.add("date"); System.out.println("Modified List: " + list); } }
Explanation
- The
Arrays.asList
method converts the array to a fixed-size list. - The
ArrayList
constructor creates a newArrayList
from the fixed-size list, resulting in a resizable list. - The list can now be modified independently of the original array.
Output:
Array: [apple, banana, cherry] List: [apple, banana, cherry] Modified List: [apple, banana, cherry, date]
Using Stream
API
The Stream
API (introduced in Java 8) provides a modern and concise way to convert an array to a list.
Example
import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class ArrayToListExample { public static void main(String[] args) { String[] array = {"apple", "banana", "cherry"}; // Convert array to list using Stream API List<String> list = Stream.of(array) .collect(Collectors.toList()); System.out.println("Array: " + Arrays.toString(array)); System.out.println("List: " + list); // Modify the list list.add("date"); System.out.println("Modified List: " + list); } }
Explanation
- The
Stream.of
method creates a stream from the array. - The
collect
method withCollectors.toList()
collects the stream elements into a newArrayList
. - The resulting list is resizable and can be modified independently of the original array.
Output:
Array: [apple, banana, cherry] List: [apple, banana, cherry] Modified List: [apple, banana, cherry, date]
Conclusion
Converting an array to a list in Java can be accomplished using various methods, each with its own advantages. The Arrays.asList
method provides a simple and direct way to create a fixed-size list backed by the original array. The ArrayList
constructor allows you to create a resizable list that can be modified independently. The Stream
API offers a modern and concise approach to converting an array to a list. Depending on your specific use case and preferences, you can choose the method that best fits your needs.
Comments
Post a Comment
Leave Comment