Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java use void method for stream mapping?

Let's say I have a void method that just does transformation on an object, without returning any value, and I want to use it in a context of a stream map() function, like this:

public List<MyObject> getList(){
    List<MyObject> objList = ...
    return objList.stream().map(e -> transform(e, e.getUuid())).collect(Collectors.toList());
}

private void transform(MyObject obj, String value){
    obj.setUuid("prefix" + value);
}

The example is made up for simplicity - the actual method is doing something else than just mucking up the UUID of an object.

Anyway, how is that possible to use a void method in a scenario like the above? Surely, I could make the method return the transformed object, but that's besides the point and is violating the design (the method should be void).

like image 689
Nestor Milyaev Avatar asked Jan 18 '17 12:01

Nestor Milyaev


People also ask

When would you use a void method?

The most common use of void method in java is when we want to change the internal state of the object but do not require the updated state. For example in VoidWithReturnExample. java , the method setName and setAge are only used to change the name and age respectively, but they don't return anything.

Can we apply stream on map in Java?

Converting only the Value of the Map<Key, Value> into Stream: This can be done with the help of Map. values() method which returns a Set view of the values contained in this map. In Java 8, this returned set can be easily converted into a Stream of key-value pairs using Set. stream() method.

How does map work in Java Stream?

The map() function is a method in the Stream class that represents a functional programming concept. In simple words, the map() is used to transform one object into other by applying a function. That's why the Stream. map(Function mapper) takes a function as an argument.

Why we use void in method?

The void keyword specifies that a method should not have a return value.


3 Answers

Seems like this is a case of forced usage of java 8 stream. Instead you can achieve it with forEach.

List<MyObject> objList = ...
objList.forEach(e -> transform(e, e.getUuid()));
return objList;
like image 152
Murat Karagöz Avatar answered Oct 08 '22 09:10

Murat Karagöz


In addition to Eugene's answer you could use Stream::map like this:

objList.stream()
   .map(e -> {
      transform(e, e.getUuid()); 
      return e;
   }).collect(Collectors.toList());

Actually, you don't want to transform your current elements and collect it into a new List.

Instead, you want to apply a method for each entry in your List.

Therefore you should use Collection::forEach and return the List.

List<MyObject> objList = ...;
objList.forEach(e -> transform(e, e.getUuid()));
return objList;
like image 39
Flown Avatar answered Oct 08 '22 09:10

Flown


If you are sure that this is what you want to do, then use peek instead of map

like image 22
Eugene Avatar answered Oct 08 '22 09:10

Eugene