SupportWriter space ↗
← Back to the journal
Core API

How do I convert an array into a collection object?

To convert array based data into List / Collection based we can use java.util.Arrays class. This class provides a static method asList(T… a) that converts array into List / Collection. package org.kodejava.util; import java.util.Arrays; import java.util.List; public class ArrayAsListExample { public static void main(String[] args) { String[] words = {"Happy", "New", "Year", "2021"}; List<String> list […]

DWayan · 02 Mar 2006 · 1 min read

Article by Wayan · Source: Kode Java ↗

To convert array based data into List / Collection based we can use java.util.Arrays class. This class provides a static method asList(T... a) that converts array into List / Collection.

package org.kodejava.util;

import java.util.Arrays;
import java.util.List;

public class ArrayAsListExample {
    public static void main(String[] args) {
        String[] words = {"Happy", "New", "Year", "2021"};
        List<String> list = Arrays.asList(words);

        for (String word : list) {
            System.out.println(word);
        }
    }
}

The results of our code are:

Happy
New
Year
2021
← Explore more notes