0
我寫了這段代碼,但現在我想重構它以便不使用遞歸。但是我無法把頭圍住它?任何想法傢伙?遞歸到迭代 - 重構
public List<ServiceDTO> findCustomerServices(String customerId) {
List<Service> serviceTree = contractService.findCustomerServices(customerId);
List<ServiceDTO> serviceDTOs = new ArrayList<ServiceDTO>();
cloneTree(serviceTree, serviceDTOs);
return serviceDTOs;
}
private void cloneTree(List<Service> services, List<ServiceDTO> clonedServices) {
for (Service service : services) {
ServiceDTO serviceDTO = new ServiceDTO();
serviceDTO.setServiceId(service.getServiceId());
serviceDTO.setCfServiceInstanceId(service.getCfServiceInstanceId());
serviceDTO.setEndDate(service.getEndDate());
serviceDTO.setStartDate(service.getStartDate());
serviceDTO.setPhoneNumber(service.getPhoneNumber());
serviceDTO.setIsPrimary(service.getIsPrimary());
serviceDTO.setDesignationNumber(service.getDesignationNumber());
serviceDTO.setServiceIdentifier(service.getServiceIdentifier());
serviceDTO.setEndDateDay(service.getEndDateDay());
serviceDTO.setEndDateMonth(service.getEndDateMonth());
serviceDTO.setEndDateYear(service.getEndDateYear());
clonedServices.add(serviceDTO);
if (service.getDependentServices() != null && !service.getDependentServices().isEmpty()) {
cloneTree(service.getDependentServices(), serviceDTO.getDependentServices());
}
}
}
你爲什麼要重構它? – shmosel
因爲它使用太多內存。每項服務都可以有許多相關的服務,只有很少的關卡。 – Dariusz
深度是否有限制? – shmosel