aristas :
I want to create a String with String.format like this String.format("Q1={}, Q2={}, Q3={}, Q4={}" list) from the values of a list that contains Integers.
private String createString(Player player){
List<Integer> answers= new ArrayList<>();
answers.add(player.getQuestion1());
answers.add(player.getQuestion2());
answers.add(player.getQuestion3());
answers.add(player.getQuestion4());
answers.add(player.getQuestion5());
answers.add(player.getQuestion6());
answers.add(player.getQuestion7());
answers.add(player.getQuestion8());
answers.add(player.getQuestion9());
answers.add(player.getQuestion10());
return String.format("Q1={}, Q2={}, Q3={}, Q4={}, Q5={}, Q6={}, Q7={}, Q8={}, Q9={}, Q10={}", answers.stream().map(a -> a.toString());
}
Naman :
Something like the following using Collectors.joining
with mapping as required witht index and answer :
return IntStream.range(0, answers.size())
.mapToObj(idx -> String.format("Q%s = %s", idx + 1, answers.get(idx)))
.collect(Collectors.joining(", "))
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=306333&siteId=1