1

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())?

3
  • 4
    "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? Commented Sep 6, 2018 at 16:36
  • Possible duplicate of Why doesn't Collectors.toList() work on primitive collections? Commented Sep 6, 2018 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. Commented Sep 6, 2018 at 16:51

4 Answers 4

3

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.

The primitive is not an acceptable parameter to 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.

Sign up to request clarification or add additional context in comments.

Comments

3

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):

This will take input as an arrays (here intArray) and return a stream of elements present in array.

2..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.

3.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. .

Comments

1

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:

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.

List<Integer> list = Arrays.stream(intArray)
             .collect(ArrayList::new, ArrayList::add, ArrayList::addAll);

It works in the following way:

  • ArrayList::new is a Supplier - a function that creates a new result container, in our case a new ArrayList.
  • ArrayList::add is a ObjIntConsumer - a function which adds an additional element into a result container.
  • ArrayList::addAll is a BiConsumer - a function for combining two values. It is used when stream is executed in parallel to combine two partial results. In this case if stream produces 2 ArrayList we need to combine them into one and proceed to the next stage.

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.

I hope this helps.

Comments

1

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).

So in your example you are converting a Stream of primitive int into a Stream<Integer> when you call .boxed()

1 Comment

There's no such thing as Stream<int> - presumably you mean IntStream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.