Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating string array using java stream

I know that for object, we can forEach the collection and update the object as we like but for immutable objects like Strings, how can we update the array with new object without converting it into an array again.

For e.g, I have an array of string. I want to iterate through each string and trim them. I would otherwise have to do something like this:

Arrays.stream(str).map(c -> c.trim()).collect(Collectors.toList())

In the end, I will get a List rather then String[] that I initially gave. Its a whole lot of processing. Is there any way I can do something similar to:

for(int i = 0; i < str.length; i++) {
        str[i] = str[i].trim();
    }

using java streams?

like image 923
ata Avatar asked Dec 15 '16 10:12

ata


People also ask

How do you update an existing array in Java?

To update or set an element or object at a given index of Java ArrayList, use ArrayList. set() method. ArrayList. set(index, element) method updates the element of ArrayList at specified index with given element.

Can we use stream on string in Java?

Conversion Using chars()The String API has a new method – chars() – with which we can obtain an instance of Stream from a String object. This simple API returns an instance of IntStream from the input String.

Can we use array with stream in Java?

The stream(T[] array) method of Arrays class in Java, is used to get a Sequential Stream from the array passed as the parameter with its elements. It returns a sequential Stream with the elements of the array, passed as parameter, as its source.


3 Answers

Streams are not intended for manipulating other data structures, especially not for updating their source. But the Java API consists of more than the Stream API.

As Alexis C. has shown in a comment, you could use Arrays.setAll(arr, i -> arr[i].trim());

There’s even a parallelSetAll that you could use when you have a really large array.

However, it might be easier to use just Arrays.asList(arr).replaceAll(String::trim);.

Keep in mind that the wrapper returned by Arrays.asList allows modifications of the wrapped array through the List interface. Only adding and removing is not supported.

like image 65
Holger Avatar answered Oct 10 '22 04:10

Holger


Use toArray :

str = Arrays.stream(str).map(c -> c.trim()).toArray(String[]::new);

The disadvantage here (over your original Java 7 loop) is that a new array is created to store the result.

To update the original array, you can re-write your loop with Streams, though I'm not sure what's the point :

IntStream.range (0, str.length).forEach (i -> {str[i] = str[i].trim();});
like image 33
Eran Avatar answered Oct 10 '22 02:10

Eran


It's not that much processing as you might think, the array has a known size and the spliterator from it will be SIZED, thus the resulting collection size will be known before processing and the space for it can be allocated ahead of time, without having to re-size the collection.

It's also always interesting that in the absence of actual tests we almost always assume that this is slow or memory hungry.

of course if you want an array as the result there is a method for that :

 .toArray(String[]::new);
like image 2
Eugene Avatar answered Oct 10 '22 04:10

Eugene