这个问题已经问过了。但今天我发现了一些奇怪的东西。以下代码:
public static List convertBucketDefinitionList(List bucketInstanceDefinitionV1List) {
List response = new ArrayList<>();
List finalResponse = new ArrayList<>();
bucketInstanceDefinitionV1List.stream().forEach(e -> {
EsbBucketInstanceDefinition esbBucketInstanceDefinition = new EsbBucketInstanceDefinition();
esbBucketInstanceDefinition.setInstanceType(e.getInstanceType());
esbBucketInstanceDefinition.setReportingGroup(e.getReportingGroup());
esbBucketInstanceDefinition.setSliceVolume(e.getSliceVolume());
esbBucketInstanceDefinition.setCounterName(e.getCounterName());
esbBucketInstanceDefinition.setSubscriberGroupId(e.getSubscriberGroupId());
// response.add(esbBucketInstanceDefinition); compiler error variable used in lambda should be final or effective final
finalResponse.add(esbBucketInstanceDefinition);
});
return finalResponse;
}
因为这很管用。看起来只有变量名finalResponse有效。为什么?这样做有效吗?