英文:
capitalizing first letters in listArray without producing void
问题
public List<String> capitalizeAllWords(ArrayList<String> words) {
return words.stream().map(word ->
word.substring(0, 1).toUpperCase() + word.substring(1))
.collect(Collectors.toList());
}
英文:
I'm trying to capitalize the first letter of each word in a List
. Currently, I'm getting a "Lambda Expression not expected here", and I'm told that I can't convert a String to a void, which I'm not trying to do. I'm trying to capitalize the first letter of each string in an arrayList; I haven't been able to determine a way to do so without selecting+capitalizing the first char (as char or as substring) and then adding the rest of the word.
public List<String> capitalizeAllWords(ArrayList<String> words) {
return words.stream().collect(Collectors.toList().forEach(word ->
word.substring(0,1).toUpperCase() + word.substring(1))
);
}
答案1
得分: 3
Use .map
before collecting.
public List<String> capitalizeAllWords(ArrayList<String> words) {
return words.stream()
.map(word -> Character.toUpperCase(word.charAt(0)) + word.substring(1))
.collect(Collectors.toList());
}
英文:
Use .map
before collecting.
public List<String> capitalizeAllWords(ArrayList<String> words) {
return words.stream()
.map(word -> Character.toUpperCase(word.charAt(0)) + word.substring(1))
.collect(Collectors.toList());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论