java IntStream cannot use collect(Collectors.toList()), compilation error, why? [duplicate]
This question already has an answer here:
Java Stream Collectors.toList() wont compile
3 answers
As below:
IntStream iStream = IntStream.range(1,4);
iStream.forEach(System.out::print);
List list1 = iStream.collect(Collectors.toList());//error!
Java 1.8 compiler gives type deduction error. Similar code could work for String type:
List<String> ls = new ArrayList<>();
ls.add("abc");
ls.add("xyz");
List list2 = ls.stream().collect(Collectors.toList());
Why? Does IntStream/LongStream/DoubleStream are not working the same way like other types? How to fix my compilation error?
java compilation java-stream collect tolist
marked as duplicate by Oleksandr, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 10:19
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Java Stream Collectors.toList() wont compile
3 answers
As below:
IntStream iStream = IntStream.range(1,4);
iStream.forEach(System.out::print);
List list1 = iStream.collect(Collectors.toList());//error!
Java 1.8 compiler gives type deduction error. Similar code could work for String type:
List<String> ls = new ArrayList<>();
ls.add("abc");
ls.add("xyz");
List list2 = ls.stream().collect(Collectors.toList());
Why? Does IntStream/LongStream/DoubleStream are not working the same way like other types? How to fix my compilation error?
java compilation java-stream collect tolist
marked as duplicate by Oleksandr, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 10:19
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Callboxed ()
before collecting.
– ETO
Nov 16 '18 at 7:47
add a comment |
This question already has an answer here:
Java Stream Collectors.toList() wont compile
3 answers
As below:
IntStream iStream = IntStream.range(1,4);
iStream.forEach(System.out::print);
List list1 = iStream.collect(Collectors.toList());//error!
Java 1.8 compiler gives type deduction error. Similar code could work for String type:
List<String> ls = new ArrayList<>();
ls.add("abc");
ls.add("xyz");
List list2 = ls.stream().collect(Collectors.toList());
Why? Does IntStream/LongStream/DoubleStream are not working the same way like other types? How to fix my compilation error?
java compilation java-stream collect tolist
This question already has an answer here:
Java Stream Collectors.toList() wont compile
3 answers
As below:
IntStream iStream = IntStream.range(1,4);
iStream.forEach(System.out::print);
List list1 = iStream.collect(Collectors.toList());//error!
Java 1.8 compiler gives type deduction error. Similar code could work for String type:
List<String> ls = new ArrayList<>();
ls.add("abc");
ls.add("xyz");
List list2 = ls.stream().collect(Collectors.toList());
Why? Does IntStream/LongStream/DoubleStream are not working the same way like other types? How to fix my compilation error?
This question already has an answer here:
Java Stream Collectors.toList() wont compile
3 answers
java compilation java-stream collect tolist
java compilation java-stream collect tolist
edited Nov 16 '18 at 11:51
Stefan Zobel
2,48231931
2,48231931
asked Nov 16 '18 at 7:44
Hind ForsumHind Forsum
3,07431742
3,07431742
marked as duplicate by Oleksandr, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 10:19
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Oleksandr, Mark Rotteveel
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 16 '18 at 10:19
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Callboxed ()
before collecting.
– ETO
Nov 16 '18 at 7:47
add a comment |
Callboxed ()
before collecting.
– ETO
Nov 16 '18 at 7:47
Call
boxed ()
before collecting.– ETO
Nov 16 '18 at 7:47
Call
boxed ()
before collecting.– ETO
Nov 16 '18 at 7:47
add a comment |
2 Answers
2
active
oldest
votes
IntStream
(along with the other primitive streams) does not have a collect(Collector)
method. Its collect method is: collect(Supplier,ObjIntConsumer,BiConsumer)
.
If you want to collect the int
s into a List
you can do:
List<Integer> list = IntStream.range(0, 10).collect(ArrayList::new, List::add, List::addAll);
Or you can call boxed()
to convert the IntStream
to a Stream<Integer>
:
List<Integer> list = IntStream.range(0, 10).boxed().collect(Collectors.toList());
Both options will box the primitive int
s into Integer
s, so which you want to use is up to you. Personally, I find the second option simpler and clearer.
add a comment |
The primitive streams don't have the same collect
method as Stream
. You can convert them to a stream of the wrapper type in order to use the collect
method that accepts a Collector
argument:
List<Integer> list1 = iStream.boxed().collect(Collectors.toList());
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
IntStream
(along with the other primitive streams) does not have a collect(Collector)
method. Its collect method is: collect(Supplier,ObjIntConsumer,BiConsumer)
.
If you want to collect the int
s into a List
you can do:
List<Integer> list = IntStream.range(0, 10).collect(ArrayList::new, List::add, List::addAll);
Or you can call boxed()
to convert the IntStream
to a Stream<Integer>
:
List<Integer> list = IntStream.range(0, 10).boxed().collect(Collectors.toList());
Both options will box the primitive int
s into Integer
s, so which you want to use is up to you. Personally, I find the second option simpler and clearer.
add a comment |
IntStream
(along with the other primitive streams) does not have a collect(Collector)
method. Its collect method is: collect(Supplier,ObjIntConsumer,BiConsumer)
.
If you want to collect the int
s into a List
you can do:
List<Integer> list = IntStream.range(0, 10).collect(ArrayList::new, List::add, List::addAll);
Or you can call boxed()
to convert the IntStream
to a Stream<Integer>
:
List<Integer> list = IntStream.range(0, 10).boxed().collect(Collectors.toList());
Both options will box the primitive int
s into Integer
s, so which you want to use is up to you. Personally, I find the second option simpler and clearer.
add a comment |
IntStream
(along with the other primitive streams) does not have a collect(Collector)
method. Its collect method is: collect(Supplier,ObjIntConsumer,BiConsumer)
.
If you want to collect the int
s into a List
you can do:
List<Integer> list = IntStream.range(0, 10).collect(ArrayList::new, List::add, List::addAll);
Or you can call boxed()
to convert the IntStream
to a Stream<Integer>
:
List<Integer> list = IntStream.range(0, 10).boxed().collect(Collectors.toList());
Both options will box the primitive int
s into Integer
s, so which you want to use is up to you. Personally, I find the second option simpler and clearer.
IntStream
(along with the other primitive streams) does not have a collect(Collector)
method. Its collect method is: collect(Supplier,ObjIntConsumer,BiConsumer)
.
If you want to collect the int
s into a List
you can do:
List<Integer> list = IntStream.range(0, 10).collect(ArrayList::new, List::add, List::addAll);
Or you can call boxed()
to convert the IntStream
to a Stream<Integer>
:
List<Integer> list = IntStream.range(0, 10).boxed().collect(Collectors.toList());
Both options will box the primitive int
s into Integer
s, so which you want to use is up to you. Personally, I find the second option simpler and clearer.
answered Nov 16 '18 at 7:51
SlawSlaw
9,36131235
9,36131235
add a comment |
add a comment |
The primitive streams don't have the same collect
method as Stream
. You can convert them to a stream of the wrapper type in order to use the collect
method that accepts a Collector
argument:
List<Integer> list1 = iStream.boxed().collect(Collectors.toList());
add a comment |
The primitive streams don't have the same collect
method as Stream
. You can convert them to a stream of the wrapper type in order to use the collect
method that accepts a Collector
argument:
List<Integer> list1 = iStream.boxed().collect(Collectors.toList());
add a comment |
The primitive streams don't have the same collect
method as Stream
. You can convert them to a stream of the wrapper type in order to use the collect
method that accepts a Collector
argument:
List<Integer> list1 = iStream.boxed().collect(Collectors.toList());
The primitive streams don't have the same collect
method as Stream
. You can convert them to a stream of the wrapper type in order to use the collect
method that accepts a Collector
argument:
List<Integer> list1 = iStream.boxed().collect(Collectors.toList());
answered Nov 16 '18 at 7:46
EranEran
290k37477563
290k37477563
add a comment |
add a comment |
Call
boxed ()
before collecting.– ETO
Nov 16 '18 at 7:47