How can I to convert Array of Integers to ArrayDeque? For example, instead to add numbers in ArrayDeque with loop, will I can to convert this Array of integers directly to ArrayDeque? Thanks in advance.
Asked
Active
Viewed 7,361 times
6
-
2`Arrays.stream(1,2,3,4,5).collect(Collectors.toCollection(ArrayDeque::new))`. – Boris the Spider Jan 06 '17 at 21:47
-
@BoristheSpider You need to call `boxed` on the `IntStream` before you can `collect` this way. – flakes Nov 22 '18 at 02:51
2 Answers
8
List<Integer> list = Arrays.asList(array);
ArrayDeque<Integer> ad = new ArrayDeque<>(list);

Enrico Giurin
- 2,183
- 32
- 30
0
Convert the array into a List
first, preserving values type casting. Then create a Deque
from a List
.
int[] array = new int[]{1,2,3,4,5};
// List<Object> list = Arrays.asList(array);
List<Integer> array = Arrays.stream(array).boxed().collect(Collectors.toList());
Deque<Integer> arrayDeque = new ArrayDeque<>(array);

Zon
- 18,610
- 7
- 91
- 99