Pages

Thursday, August 13, 2020

How To Convert ArrayList To String Array In Java 8 - toArray() Example

1. Overview

In this article, You're going to learn how to convert ArrayList to String[] Array in Java. ArrayList is used to store the List of String using the toArray() and Stream API toArray() method.

How To Convert ArrayList To String Array In Java 8 - toArray() Example


2. ArrayList to String Without Using API Methods

This is a simple approach by running a for loop from index 0 to the last index of the ArrayList and next Create a new String with the size of ArrayList size. 

Finally, assign each index value from ArrayList to String array at the same index.

Look at the below example program. Read the comments given in the program 

package com.javaprogramto.java8.arraylist.toarray;

import java.util.ArrayList;
import java.util.List;

public class NormalApproach {

    public static void main(String[] args) {

        // Created a List with type String
        List<String> listOfStrings = new ArrayList<>();

        // Added 5 string values to ArrayList
        listOfStrings.add("String One");
        listOfStrings.add("String Two");
        listOfStrings.add("String Three");
        listOfStrings.add("String Four");
        listOfStrings.add("String Five");

        // Created string array with the size of List
        String[] stringArray = new String[listOfStrings.size()];

        // Adding list values into ArrayList
        for (int i = 0; i < listOfStrings.size(); i++) {
            stringArray[i] = listOfStrings.get(i);
        }

        // Printing the values of Array.
        for (int i = 0; i < stringArray.length; i++) {
            System.out.println("String value at index "+i+" is "+stringArray[i]);
        }
    }
}
Output:
String value at index 0 is String One
String value at index 1 is String Two
String value at index 2 is String Three
String value at index 3 is String Four
String value at index 4 is String Five

3. Using List.toArray() Method


List api has a builtin method to covert List<T> to Array<T>[] array, In our example, T is the type of String. So, When List.toArray() method is called it converts ArrayList to String array.
package com.javaprogramto.java8.arraylist.toarray;

import java.util.ArrayList;
import java.util.List;

public class ListToArrayExample {

    public static void main(String[] args) {

        // Created a List with type String
        List<String> listOfStrings = new ArrayList<>();

        // Added 5 string values to ArrayList
        listOfStrings.add("String One");
        listOfStrings.add("String Two");
        listOfStrings.add("String Three");
        listOfStrings.add("String Four");
        listOfStrings.add("String Five");

        // Created string array with the size of List and calling toArray() method
        String[] stringArray = listOfStrings.toArray(new String[listOfStrings.size()]);

        // Printing the values of Array.
        for (int i = 0; i < stringArray.length; i++) {
            System.out.println("String value at index "+i+" is "+stringArray[i]);
        }
    }

}

Output:
String value at index 0 is String One
String value at index 1 is String Two
String value at index 2 is String Three
String value at index 3 is String Four
String value at index 4 is String Five

4. Using Java 8 Stream.toArray() Method


Java 8 Stream API is added with a toArray() method which takes IntFunction as a Functional Interface argument and returns the array with the type of value in the List.
import java.util.ArrayList;
import java.util.List;

public class StreamToArrayExample {

    public static void main(String[] args) {

        // Created a List with type String
        List<String> listOfStrings = new ArrayList<>();

        // Added 5 string values to ArrayList
        listOfStrings.add("String One");
        listOfStrings.add("String Two");
        listOfStrings.add("String Three");
        listOfStrings.add("String Four");
        listOfStrings.add("String Five");

        // Using Stream api toArray() method and passing the String array object with method reference.
        String[] stringArray = listOfStrings.stream().toArray(String[]::new);

        // Printing the values of Array.
        for (int i = 0; i < stringArray.length; i++) {
            System.out.println("String value at index "+i+" is "+stringArray[i]);
        }
    }


}

Output:

This program also generates the same output as used List.toArray() and custom logic.
String value at index 0 is String One
String value at index 1 is String Two
String value at index 2 is String Three
String value at index 3 is String Four
String value at index 4 is String Five

5. Using Arrays.toArray() and Arrays.copyof() Methods


Another way to convert List to String with Arrays.copyof() method.
ArrayList<String> arrayList = new ArrayList<String>();
Object[] objectList = arrayList.toArray();
String[] stringArray =  Arrays.copyOf(objectList,objectList.length,String[].class);

6. Conclusion


In this article, You've learned about different ways to do the conversion from ArrayList to String[] Array using java normal and Stream API methods.

As usual, all programs are posted over GitHub


No comments:

Post a Comment

Please do not add any spam links in the comments section.