0

I use this code to return a list from REST endpoint:

private String getCurrencies(Integer id) {      
    List<CurrencyDTO> list = null;
    try {
        list = StreamSupport.stream(currenciesService.getCurrencyByContractID(Integer.valueOf(id)).spliterator(), false)
                   .map(currencies_mapper::toDTO)
                   .collect(Collectors.toList());
    } catch (Exception e) {
        e.printStackTrace();
    }

    return list.toString();
}

DTO:

public class CurrencyDTO {

    private Integer id;

    private Integer contract_id;

    private String code;
    ...
}

How I can get a list of values from code values? Something like that:

String list = "code, code, code, code, code";
Sami Ahmed Siddiqui
  • 2,328
  • 1
  • 16
  • 29
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

1 Answers1

2

If you implement toString() of CurrencyDTO to simply return the code, then list.toString() will return [code, code, code, code, code]

Otherwise use streams:

list.stream().map(CurrencyDTO::getCode).collect(Collectors.join(", "))
Andreas
  • 154,647
  • 11
  • 152
  • 247