How can I add all the values in the string list to a single string?

Viewed 90

restApplication.getPurposeOfTheLoans () is a list of string type. limit.getPurpose () is a single string value. I want to add all string statements in this list end to end and set them to the limit.setPurpose() statement. How can I do that? I shared my sample code below.

Java sample

String pOfLoan = "";
for (int i = 0; i < restApplication.getPurposeOfTheLoans().size(); i++) {
    pOfLoan.concat(restApplication.getPurposeOfTheLoans().get(i) + " ");
    limit.setPurpose(pOfLoan);
}
2 Answers

You can use stream api collect:

pOfLoan = restApplication.getPurposeOfTheLoans().stream() // stream
              .collect(Collectors.joining(" ")); // join 

Better alternative:

pOfLoan = String.join(" ", restApplication.getPurposeOfLoans());
String pOfLoan = ""; 
for(int i=0;i<restApplication.getPurposeOfTheLoans().size();i++){ 
pOfLoan = pOfLoan.concat(restApplication.getPurposeOfTheLoans().get(i)+" "); 
}
limit.setPurpose(pOfLoan); 

you forgot to assign return value of concat

Related