Search code examples
javaspringdto

how can use GetListSomething() with stream


    package com.eukolos.restaurant.dto;

import com.eukolos.restaurant.model.Table; import org.springframework.stereotype.Component;

@Component public class AllTableResponseConverter {

public AllTableResponse convert(Table table ) {

    AllTableResponse allTableResponse = new AllTableResponse();
    allTableResponse.setId(table.getId());
    allTableResponse.setNumber(table.getNumber());
allTableResponse.setAccountList(table.getAccountList().stream().map(AccountIdResponseConverter::convert).collect(Collectors.toList()));//

    return allTableResponse;
}

}` getAccountList() cant use with stream how can i handle?


Solution

  • Can you test this code below? Is this what you want to achieve?

    @Component 
    public class AllTableResponseConverter { 
    
     public AllTableResponse convert(Table table ) {
            
            List<AccountIdResponseConverter> convertedAccounts = new ArrayList<>();
            
            AllTableResponse allTableResponse = new AllTableResponse();
            allTableResponse.setId(table.getId());
            allTableResponse.setNumber(table.getNumber());
            for(Table t : table.getAccountList()) {
                    AccountIdResponseConverter converter = new AccountIdResponseConverter();
                    convertedAccounts.add(converter.convert(t));
            }       
            allTableResponse.setAccountList(convertedAccounts);
        
            return allTableResponse;
        }
    }