Converting Arrays to List
Converting Arrays to List
I've got small question about converting an array to a list. Let's say I've got an array of Strings. If I'd like to have it as a list I code sth like this:
List<String> list = new ArrayList<>(Arrays.asList(stringArray));
But If I got the same situation but my array was an array of ints I'd had to do converting this way:
List<Integer> list = Arrays.stream(intArray).boxed().collect(Collectors.toList());
Why? What's the meaning of .boxed() and .collect(Collectors.toList())?
Possible duplicate of Why doesn't Collectors.toList() work on primitive collections?
– Vivek Pakmode
Sep 6 '18 at 16:43
Note that
.collect(Collectors.toList()) does not necessarily return an ArrayList; it could be some other kind of list. I presume you specifically want an ArrayList because you create one in your first code snippet.– DodgyCodeException
Sep 6 '18 at 16:51
.collect(Collectors.toList())
ArrayList
4 Answers
4
https://howtodoinjava.com/java8/java8-boxed-intstream/
To convert a stream of primitives, you must first box the elements in their wrapper class and then collect them. This type of stream in called boxed stream.
So, int is a primitive version of Integer (i.e. its not an object reference). So, you have to convert the primitive data type to the Integer object reference.
int
Integer
The primitive is not an acceptable parameter to Arrays.asList().
Arrays.asList()
As for collect() - That is just the standard method to convert a stream of elements back into a result collection. You could make it int a list, a set, a map (if you had a value you'd like to put next to it), or whatever.
First of all why you need the second way is because you can't have List of primitive data type ,as the String is an object in java thats why you were able to convert array of string to list of string using 1st technique, but since int is primitive data type thats'y you need to convert it to Integer first then create a list.
Let me split this whole line in three part
List<Integer> list=Arrays.stream(intArray).boxed().collect(Collectors.toList());
1.Arrays.stream(intArray):
Arrays.stream(intArray)
This will take input as an arrays (here intArray) and return a stream
of elements present in array.
2..boxed() :
.boxed()
Boxing and unboxing in java is conversion of primitive data type to
Wrapper(reference data type) and vice-versa. now this methos boxed()
works on stream of input data and return an stream of Integer after
boxing each element in the stream to Integer.
boxed()
3.collect(Collectors.toList()):
collect(Collectors.toList()):
now that you have got stream of Integer from boxed() you need to
store it to some collection. collect() works on stream of data and
the paramter passed to it tells the type collections to which data of
the stream should be mapped to. in your case it is list so
Collector.toList() is used. .
boxed()
collect()
Collector.toList()
Arrays.stream(intArray) returns an IntStream - a sequence of primitive int-valued elements. Now if we check javadocs for IntStream we can see that there isn't method collect which accepts an instance of Collector. On the contrary there is the following method:
Arrays.stream(intArray)
IntStream
IntStream
collect
Collector
IntStream#collect(Supplier,ObjIntConsumer,BiConsumer)
Performs a mutable reduction operation on the elements of this stream.
You still can transform your IntStream into a List without using boxed.
IntStream
List
boxed
List<Integer> list = Arrays.stream(intArray)
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
It works in the following way:
ArrayList::new
ArrayList::add
ArrayList::addAll
ArrayList
All these 3 elements could be abstracted into one single call using collect(Collectors.toList()). But in order to use a Collector an primitive stream IntStream must be converted to more general Stream<Integer>. This could be done using boxed() method which just performs this conversion.
collect(Collectors.toList())
Collector
IntStream
Stream<Integer>
boxed()
I hope this helps.
It's when a primitive int when converted to it's boxed equivalent Integer class. All Java primitives have a boxed equivalent class (boolean -> Boolean, long -> Long, etc).
int
Integer
So in your example you are converting a Stream of primitive int into a Stream<Integer> when you call .boxed()
Stream
int
Stream<Integer>
.boxed()
There's no such thing as
Stream<int> - presumably you mean IntStream.– DodgyCodeException
Sep 6 '18 at 16:46
Stream<int>
IntStream
Yes you're right, answer updated to avoid confusion
– Brad
Sep 6 '18 at 20:16
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
"What's the meaning of .boxed() and .collect(Collectors.toList())?" Did you read the documentation for those methods? Which part of that documentation was not clear?
– csmckelvey
Sep 6 '18 at 16:36