我使用以下代码从REST端点返回列表:
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;
...
}
如何从代码值中获取值列表?像这样:
String list = "code, code, code, code, code";
答案 0 :(得分:2)
如果您实现toString()
中的CurrencyDTO
仅返回code
,则list.toString()
将返回[code, code, code, code, code]
否则使用流:
list.stream().map(CurrencyDTO::getCode).collect(Collectors.join(", "))