🎓 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 (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Java, it's common to work with arrays and lists. While arrays have a fixed size, lists are more flexible. Java 8 provides an easy and efficient way to convert an array to a list using Streams and utility methods. In this guide, we will explore different ways to convert an array to a list in Java 8.
Problem Statement
You have an array, and you want to convert it into a list. Write a program that:
- Takes an array as input.
- Converts the array to a list.
- Displays the list.
Example:
- Input:
{"Java", "Python", "C++"} - Output:
["Java", "Python", "C++"]
Solution Steps
- Define the Input Array: Define an array that you want to convert to a list.
- Use Utility Methods: Use
Arrays.asList()or Streams to convert the array to a list. - Display the Result: Print the converted list.
Method 1: Using Arrays.asList()
Java Program
import java.util.Arrays;
import java.util.List;
public class ArrayToListExample1 {
public static void main(String[] args) {
// Step 1: Define the input array
String[] array = {"Java", "Python", "C++"};
// Step 2: Convert the array to a list using Arrays.asList()
List<String> list = Arrays.asList(array);
// Step 3: Display the result
System.out.println("List: " + list);
}
}
Output
List: [Java, Python, C++]
Explanation
Arrays.asList(array)converts the array into a fixed-size list. This method is simple but has a limitation: the returned list is fixed in size, meaning you cannot add or remove elements.
Method 2: Using Java 8 Streams
Java Program
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class ArrayToListExample2 {
public static void main(String[] args) {
// Step 1: Define the input array
String[] array = {"Java", "Python", "C++"};
// Step 2: Convert the array to a list using Java 8 Streams
List<String> list = Arrays.stream(array)
.collect(Collectors.toList());
// Step 3: Display the result
System.out.println("List: " + list);
}
}
Output
List: [Java, Python, C++]
Explanation
Arrays.stream(array)creates a stream from the array.Collectors.toList()collects the elements from the stream into a new list.- This method allows you to create a modifiable list, meaning you can add or remove elements from the list.
Method 3: Using Collections.addAll()
Java Program
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ArrayToListExample3 {
public static void main(String[] args) {
// Step 1: Define the input array
String[] array = {"Java", "Python", "C++"};
// Step 2: Convert the array to a list using Collections.addAll()
List<String> list = new ArrayList<>();
Collections.addAll(list, array);
// Step 3: Display the result
System.out.println("List: " + list);
}
}
Output
List: [Java, Python, C++]
Explanation
Collections.addAll()adds all elements of the array into a newArrayList.- This method creates a modifiable list, allowing you to add or remove elements later.
Output Example
Example 1:
For the input {"Java", "Python", "C++"}, the output for all methods will be:
List: [Java, Python, C++]
Example 2:
For the input {"Apple", "Banana", "Orange"}, the output will be:
List: [Apple, Banana, Orange]
Conclusion
There are several ways to convert an array to a list in Java 8. Using Arrays.asList() is quick and simple but results in a fixed-size list. On the other hand, using Java 8 Streams or Collections.addAll() allows for the creation of modifiable lists. Depending on your requirements (fixed or modifiable list), you can choose the method that best suits your needs.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment