tzortzik
tzortzik

Reputation: 5133

creating new object inside a stream and use the setters

I am trying to create an object inside the stream but I do not want to define another constructor for the object; I would like to use the setters to put the values where needed. Is this possible?

pe.setExts(pDTO.getExts().stream().map(s->new P(arg1, arg2, ..., arg12)))...;

As you can see I have a lot of arguments and some of them requires some processing. I want to avoid doing this processing until it is necessary.

What I am looking for may be something like this (I am not sure how to write the function; I think an anonymous function would be great here):

pe.setExts(pDTO.getExts().stream().map(s->{
    P p = new P();
    s->setExt1(p.getExt1());
    ...
    List<V> l = p.getExt12();
    List<X> finalL = null;
    [processing list l, populating finalL]
    s->setExt12(finalL);
}));

Upvotes: 1

Views: 1739

Answers (1)

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

You can't use a constructor that's not there.

I'd suggest to write a method that would take the s variable and return a P one. Something like:

public static P generatePfromS(S s) {
     //convert S to P
}

and then you can just do:

pe.setExts(pDTO.getExts().stream().map(YourClass::generatePFromS).collect(toList()));

Upvotes: 3

Related Questions