1

I have a list<P> (java.util.list) . Now I want to replace elements in list<A> based on two conditions.

If element == 'a' replace with 'x' and if element == 'b' replace with 'y'. x and y are of type Q.

What is the cleanest / efficient way to do this? Preferably, I do not want to iterate using a for loop. I can use a stream/replaceAll but that would mean violating the type of the list.

2
  • Please be more specific. Generally, the approach is to transform the entire List<P> into a List<A> or similar. Commented Sep 4, 2020 at 3:08
  • the whole of elements you have are either =a or =b? if it is not in this way , perhaps you have to use a generic list Commented Sep 4, 2020 at 3:17

1 Answer 1

1

I figured it out. I can use (pseudocode) (list<A>).stream().forEach(r->{condition?list<B>.add(x):list<B>.add(y)})

init list< B > before this.

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

Comments

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.