当List<B>以复数字段作为keyList与List<A>进行match,match成功时,将List<B>根据条件将部分字段更新后生成新的list,并且要求不改变原来List<B>的值。用Java8写一个满足需求的共通方法
时间: 2024-05-08 11:16:10 浏览: 147
假设有如下的类定义:
```
class A {
private int id;
private String name;
// getters and setters
}
class B {
private int id;
private String name;
private int count;
// getters and setters
}
```
则可以使用如下的Java8方法实现上述需求:
```
public static List<B> updateBList(List<B> bList, List<A> aList) {
Map<Integer, A> aMap = aList.stream().collect(Collectors.toMap(A::getId, Function.identity()));
return bList.stream()
.map(b -> aMap.containsKey(b.getId()) ? updateB(b, aMap.get(b.getId())) : b)
.collect(Collectors.toList());
}
private static B updateB(B b, A a) {
if (a.getName() != null) {
b.setName(a.getName());
}
if (a.getId() != b.getId()) {
b.setId(a.getId());
}
// update other fields as needed
return b;
}
```
这个方法接受两个参数:一个是List<B>,另一个是List<A>。它首先将List<A>转换为Map<Integer, A>,其中Integer是A的id字段,A是对应的对象。然后对于List<B>中的每个B对象,判断是否在Map中存在对应的A对象。如果存在,则调用updateB方法将B对象更新为新的值;否则不做修改。最终返回更新后的List<B>对象。注意,这个方法不会改变原始的List<B>对象,而是返回一个新的List。
阅读全文
相关推荐















<html><head><meta http-equiv="Content-Type" content="text/html; charset=
=iso-8859-1">
<title>Trioptics Certificate</title>
<base href="file:///C:/Program%20Files%20(x86)/TRIOPTICS%20GmbH/MTF-LAB%2=
05/Certificates/"></base></head>
<body id="IBodyCert" class="CBodyCert">
ImageMaster - Certificate
Company : ******
Operator : ******
Time/Date : 13:30:33 June 12, 2025
Sample ID : ******
Measure Program : MTF vs. Field
Temperature : 20°C
Measured with : TRIOPTICS - MT=
F-LAB - Vers. 5.16.1
Instrument S/N : 09-113-0519
Comments : SN215U0540
Measurement Parameter: MTF vs. Image Height
Setup Type : Object Infinite / Image Finite
EFL (Collimator): 50 mm
Wavelength : 940 nm (NIR)
EFL (Sample) : 1.5550 mm
F-Number : 2.0000
Object Angle : -0.0198 °
Focus Position : 85.8471
Sample Azimuth : 0.0 °
Measurement Graph: MTF vs. Image Height
<right>
</right>
Measurement Table: MTF vs. Image Height
Image Height (mm)
MTF
0.42800
0.29960
-0.00114
-0.29960
-0.42800
Legend
Tan 100(lp/mm)
0.599
0.677
0.668
0.703
0.645
— — —
Sag 100(lp/mm)
0.558
0.659
0.673
0.688
0.642
————
Measurement Parameter: MTF vs. Object Angle
Setup Type : Object Infinite / Image Finite
EFL (Collimator): 50 mm
Wavelength : 940 nm (NIR)
EFL (Sample) : 1.5550 mm
F-Number : 2.0000
Object Angle : -0.0198 °
Focus Position : 85.8471
Sample Azimuth : 0.0 °
Measurement Graph: MTF vs. Object Angle
<right>
</right>
Measurement Table: MTF vs. Object Angle
Object Angle (?
MTF
-15.33590
-10.72937
0.00072
10.77662
15.40352
Legend
Tan 100(lp/mm)
0.599
0.677
0.668
0.703
0.645
— — —
Sag 100(lp/mm)
0.558
0.659
0.673
0.688
0.642
————
</body></html> 解析这个

Measurement Parameter: MTF vs. Image Height Setup Type : Object Infinite / Image Finite EFL (Collimator): 50 mm Wavelength : 940 nm (NIR) EFL (Sample) : 1.5550 mm F-Number : 2.0000 Object Angle : -0.0198 ° Focus Position : 85.8471 Sample Azimuth : 0.0 °
Measurement Graph: MTF vs. Image Height
<right>
Measurement Table: MTF vs. Image Height
Measurement Parameter: MTF vs. Object Angle Setup Type : Object Infinite / Image Finite EFL (Collimator): 50 mm Wavelength : 940 nm (NIR) EFL (Sample) : 1.5550 mm F-Number : 2.0000 Object Angle : -0.0198 ° Focus Position : 85.8471 Sample Azimuth : 0.0 °
Measurement Graph: MTF vs. Object Angle
<right>
Measurement Table: MTF vs. Object Angle

