Converting Array to List in Java

Converting array to list in Java

In your example, it is because you can't have a List of a primitive type. In other words, List<int> is not possible.

You can, however, have a List<Integer> using the Integer class that wraps the int primitive. Convert your array to a List with the Arrays.asList utility method.

Integer[] numbers = new Integer[] { 1, 2, 3 };
List<Integer> list = Arrays.asList(numbers);

See this code run live at IdeOne.com.

Convert list to array in Java

Either:

Foo[] array = list.toArray(new Foo[0]);

or:

Foo[] array = new Foo[list.size()];
list.toArray(array); // fill the array

Note that this works only for arrays of reference types. For arrays of primitive types, use the traditional way:

List<Integer> list = ...;
int[] array = new int[list.size()];
for(int i = 0; i < list.size(); i++) array[i] = list.get(i);

Update:

It is recommended now to use list.toArray(new Foo[0]);, not list.toArray(new Foo[list.size()]);.

From JetBrains Intellij Idea inspection:

There are two styles to convert a collection to an array: either using
a pre-sized array (like c.toArray(new String[c.size()])) or
using an empty array (like c.toArray(new String[0]).

In
older Java versions using pre-sized array was recommended, as the
reflection call which is necessary to create an array of proper size
was quite slow. However since late updates of OpenJDK 6 this call
was intrinsified, making the performance of the empty array version
the same and sometimes even better, compared to the pre-sized
version. Also passing pre-sized array is dangerous for a concurrent or
synchronized collection as a data race is possible between the
size and toArray call which may result in extra nulls
at the end of the array, if the collection was concurrently shrunk
during the operation.

This inspection allows to follow the
uniform style: either using an empty array (which is recommended in
modern Java) or using a pre-sized array (which might be faster in
older Java versions or non-HotSpot based JVMs).

How to convert int[] into List Integer in Java?

There is no shortcut for converting from int[] to List<Integer> as Arrays.asList does not deal with boxing and will just create a List<int[]> which is not what you want. You have to make a utility method.

int[] ints = {1, 2, 3};
List<Integer> intList = new ArrayList<Integer>(ints.length);
for (int i : ints)
{
intList.add(i);
}

Convert String array to ArrayList

Use this code for that,

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

public class StringArrayTest {

public static void main(String[] args) {
String[] words = {"ace", "boom", "crew", "dog", "eon"};

List<String> wordList = Arrays.asList(words);

for (String e : wordList) {
System.out.println(e);
}
}
}

Create ArrayList from array

new ArrayList<>(Arrays.asList(array));

How can I convert an array to a list without using Arrays.asList() method or Java List Interface

One solution is to loop through the array and simply fill you List:

// your array of Nodes
Node<?>[] nodeArray = new Node[]{node1,node2,node3,node4,node5};

// your List
List<?> myList = new List<>();

for(Node<?> node : nodeArray)
{
myList.addLast(node); // use addLast() to keep the same order as the array
}

Convert an array to list with specific range in Java 8

You can use Stream.skip():

List<String> list = Arrays.stream(optArr).skip(1).collect(Collectors.toList());

Array to List or List to Array, which conversion is faster?

Arrays.asList is faster because it does not copy data - it returns an object wrapping the array passed to it, and implementing the List interface. Collection.toArray() copies the data to the array, so it runs in O(N) instead of O(1).

Convert array of Strings to list of Integers?

You only need to stream once.

Instead of using int Integer::parseInt(String s), you should use Integer Integer::valueOf(String s), so you don't have to call boxed() or rely on auto-boxing.

Then use collect(Collectors.toList()) directly, instead of creating intermediate array first.

List<Integer> allAnswerList = Arrays.stream(allAnswers)    // stream of String
.map(Integer::valueOf) // stream of Integer
.collect(Collectors.toList());


Related Topics



Leave a reply



Submit