Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to make a List from a primitive array

int[] values={4,5,2,3,42,60,20};
List<Integer> greaterThan4 =
Arrays.stream(values)
        .filter(value -> value > 4)
        .collect(Collectors.toList());

But the last function collect gives me an error because it wants other arguments. It wants 3 arguments Supplier, ObjIntConsumer and BiConsumer.

I don't understand why it wants 3 arguments when I have seen different examples that just use collect(Collectors.toList()); and get the list.

What I'm doing wrong?

share|improve this question

3 Answers 3

up vote 8 down vote accepted

Yes this is because Arrays.stream returns an IntStream. You can call boxed() to get a Stream<Integer> and then perform the collect operation.

List<Integer> greaterThan4 = Arrays.stream(values)
                                   .filter(value -> value > 4)
                                   .boxed()
                                   .collect(Collectors.toList());
share|improve this answer
3  
+ 1 for finding the real problem and the real solution –  fge Dec 19 '14 at 19:46

You can change int[] values={4,5,2,3,42,60,20}; to Integer[] values={4,5,2,3,42,60,20}; because currently you are passing an array of primitive type(int) but should you pass array of object i.e. Integer

share|improve this answer

You're using an array of primitives for one thing, not Integer. I suggest you use Arrays.asList(T...) like

Integer[] values={4,5,2,3,42,60,20};
List<Integer> al = new ArrayList<>(Arrays.asList(values));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.