In Java it is not possible to initialize a List
in the same way as an array, using the syntax int v[] = {1, 2, 3}
. There is even a proposal for language to have this resource, but for now the only way is to use native API methods (listed below and in the other responses).
Create the list
You can pass the values directly to the method java.util.Arrays.asList
:
List<Integer> lista = Arrays.asList(1, 2, 3, 4, 15);
If you already have an array of java.lang.Integer
with the values, it is also possible to pass it directly, as has been said in the other answers:
Integer v[] = { 1, 2, 3, 4, 15 };
List<Integer> lista = Arrays.asList(v);
But attention, if the array is int
(and not of Integer
), That’s not gonna work:
int v[] = { 1, 2, 3, 4, 15 };
List<Integer> lista = Arrays.asList(v);
Actually the above code doesn’t even compile. This is because, when asList
is called with an array of int
(and not of Integer
), the return is a List<int[]>
(a list of arrays of int
, that is, a list in which each element is an array), as already explained in this answer. And as I’m attributing the return of asList
in a List<Integer>
, the code does not compile.
In this case, the way is to do good old loop, adding the values one by one:
int v[] = { 1, 2, 3, 4, 15 };
List<Integer> lista = new ArrayList<Integer>();
for (int i : v) {
lista.add(i);
}
From Java 8 it is also possible to use streams:
int v[] = { 1, 2, 3, 4, 15 };
List<Integer> lista = Arrays
// cria o stream
.stream(v)
// convert int para Integer
.boxed()
// transforma em uma List
.collect(Collectors.toList());
First I use the method stream
, that by receiving a int[]
, will return a java.util.stream.IntStream
. The following is the method boxed
converts each int
for their respective Integer
. And finally, the method collect
gets a collector. In the case, java.util.stream.Collectors.toList()
causes the stream is "collected" to a list, resulting in a List<Integer>
.
From the Java 9 it is also possible to use java.util.List.of
:
// List.of() disponível no Java >= 9
List<Integer> lista = List.of(1, 2, 3, 4, 15);
The same observations of Arrays.asList
also apply: if you pass an array of int
as a parameter, the return will be a List<int[]>
, then it only works if you pass an array of Integer
.
The difference is that List.of
returns an immutable list, while Arrays.asList
nay:
// List.of() retorna lista imutável
List<Integer> lista = List.of(1, 2, 3, 4, 15);
// se tentar modificar elementos, lança java.lang.UnsupportedOperationException
lista.set(0, 9);
// Arrays.asList() retorna lista mutável
List<Integer> lista = Arrays.asList(1, 2, 3, 4, 15);
// posso modificar elementos sem problemas
lista.set(0, 9);
There are other differences listed in this answer.
Print the list
The easiest way is to simply print the entire list:
System.out.println(lista);
This prints all your elements at once:
[1, 2, 3, 4, 15]
But if you want to print one at a time, without the commas and brackets etc, make a for
:
for (Integer i : lista) {
System.out.println(i);
}
In this case, you will print one per line:
1
2
3
4
15
All answers gave landing options of how to do the initialization of an Arraylist already with elements but none gave the answer to the user’s question, which is no, it is not possible to start Arraylist directly with values like a common array.
– user28595
@Articunol Really, I (and others too, by the way) focused so much on "how to do" that I didn’t pay attention to this detail. I updated my answer with this information :-)
– hkotsubo