Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collect Java8 IntStream into Deque interface

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());
like image 683
maheryhaja Avatar asked May 18 '18 12:05

maheryhaja


People also ask

How do I collect IntStream?

1. Collecting IntStream into Collection using Boxed Stream. Using boxed() method of IntStream , LongStream or DoubleStream e.g. IntStream. boxed(), we can get stream of wrapper objects which can be collected by Collectors methods.

What does collect () do in Java?

collect() is one of the Java 8's Stream API's terminal methods. It allows us to perform mutable fold operations (repackaging elements to some data structures and applying some additional logic, concatenating them, etc.) on data elements held in a Stream instance.

How do you use groupingBy collectors?

The groupingBy() method of Collectors class in Java are used for grouping objects by some property and storing results in a Map instance. In order to use it, we always need to specify a property by which the grouping would be performed. This method provides similar functionality to SQL's GROUP BY clause.


1 Answers

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));
like image 60
Eugene Avatar answered Sep 17 '22 04:09

Eugene