简体   繁体   中英

How to replace an element in a java.util.List with an element of another type?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM