14

How to collect Java8 IntStream into Deque interface? I can perform this kind of operation with List like that:

List<Integer> integerList = IntStream.of(1, 2, 3)
                                     .boxed()
                                     .collect(Collectors.toList());
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
maheryhaja
  • 1,617
  • 11
  • 18

2 Answers2

20

You can't collect to an interface, but to an implementation of it (as long as it is a Collection) via Collectors.toCollection

 Deque<Integer> d = IntStream.of(1, 2)
            .boxed()
            .collect(Collectors.toCollection(ArrayDeque::new));
Eugene
  • 117,005
  • 15
  • 201
  • 306
7

use Collectors.toCollection to specify the collection you want, example:

.collect(Collectors.toCollection(ArrayDeque::new));

or any other implementation of the Deque interface.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126