
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to convert collection into array
The Collection is a framework that provides architecture to store and manipulate a group of objects. Java Collections can achieve all the operations that you perform on data, such as searching, sorting, insertion, manipulation, and deletion.
In this article, we will understand how to convert a collection into an array in Java. Following are the ways to convert a collection into an array in Java:
- Using Iteration
- Using toArray() Method
- Using Streams
Collection to Array using Iteration
The naive way to convert a collection into an array is by traversing the collection and adding each element in the array. We can use a loop to iterate through the collection and copy each element into an array. But this is not ideal for large collections as it can be inefficient.
Example
In the below example, we will convert a collection into an array using iteration:
import java.util.ArrayList; import java.util.Collection; import java.util.List; public class CollectionToArray { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("JavaScript"); list.add("C++"); list.add("Swift"); System.out.println("Original List: " + list); String[] array = new String[list.size()]; int index = 0; for (String element : list) { array[index++] = element; } System.out.println("Converted Array: "); for (String element : array) { System.out.print(element + " "); } } }
Following is the output of the above code:
Original List: [Java, Python, JavaScript, C++, Swift] Converted Array: Java Python JavaScript C++ Swift
Collection to Array using toArray() Method
toArray() is a method of the Collection interface that is used for converting any collection into an array. We pass the collection as a parameter to this method, and it returns an array of the collection elements.
Example
In the below example, we will convert a collection into an array using the toArray() method:
import java.util.ArrayList; import java.util.Collection; import java.util.List; public class CollectionToArray { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("JavaScript"); list.add("C++"); list.add("Swift"); System.out.println("Original List: " + list); String[] array = list.toArray(new String[0]); System.out.println("Converted Array: "); for(String element : array) { System.out.print(element + " "); } } }
Following is the output of the above code:
Original List: [Java, Python, JavaScript, C++, Swift] Converted Array: Java Python JavaScript C++ Swift
Collection to Array using Streams
Another way to convert a collection into an array is by using the Stream API. We have a similar method, toArray(,) in the Stream interface, which can be used to convert a stream into an array.
Example
In the below example, we will convert a collection into an array using the Stream API:
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class CollectionToArrayUsingStreams { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("JavaScript"); list.add("C++"); list.add("Swift"); System.out.println("Original List: " + list); String[] array = list.stream().toArray(String[]::new); System.out.println("Converted Array: "); for(String element : array) { System.out.print(element + " "); } } }
Following is the output of the above code:
Original List: [Java, Python, JavaScript, C++, Swift] Converted Array: Java Python JavaScript C++ Swift