[英]How to replace an element in a java.util.List with an element of another type?
I have a list<P>
(java.util.list) .我有一个
list<P>
(java.util.list) 。 Now I want to replace elements in list<A>
based on two conditions.现在我想根据两个条件替换
list<A>
的元素。
If element == 'a' replace with 'x' and if element == 'b' replace with 'y'.如果元素 == 'a' 替换为 'x',如果元素 == 'b' 替换为 'y'。 x and y are of type Q.
x 和 y 属于 Q 类型。
What is the cleanest / efficient way to do this?最干净/最有效的方法是什么? Preferably, I do not want to iterate using a for loop.
最好,我不想使用 for 循环进行迭代。 I can use a stream/replaceAll but that would mean violating the type of the list.
我可以使用 stream/replaceAll 但这意味着违反列表的类型。
I figured it out.我想到了。 I can use (pseudocode)
(list<A>).stream().forEach(r->{condition?list<B>.add(x):list<B>.add(y)})
我可以使用 (伪代码)
(list<A>).stream().forEach(r->{condition?list<B>.add(x):list<B>.add(y)})
init list< B > before this. init list<B> 在此之前。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.