1

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); })); 

1 Answer 1

3

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())); 
Sign up to request clarification or add additional context in comments.

2 Comments

I think my classes don't need to know about each other. One is P and the other one is a data transfer object PDTO. Doing your way, it means to create a third class which will contain that conversion method. Another thing here is that I am trying to use this stream inside a PPDTOAdapter. Or should I create that method inside the adapter and call it from there?
Based on your solution, I've given myself the answer in my previous comment. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.