简体   繁体   中英

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

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);
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM