6

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.

2
  • 2
    Arrays.stream(1,2,3,4,5).collect(Collectors.toCollection(ArrayDeque::new)). Commented Jan 6, 2017 at 21:47
  • @BoristheSpider You need to call boxed on the IntStream before you can collect this way. Commented Nov 22, 2018 at 2:51

2 Answers 2

8
 List<Integer> list = Arrays.asList(array);
 ArrayDeque<Integer> ad = new ArrayDeque<>(list);
Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.