英文:
How to return Objects ArrayList element to String arrayList in java 8
问题
我尝试将Objects Array List
元素添加到String Array List
中,我正在使用下面的代码,但它显示语法错误。有人可以帮我吗?
List<String> sample = request.getItemList().forEach(object -> {
return sample.add(object.getProductGroup());
});
英文:
I am trying to add Objects Array List
element to String Array List
and I am using below code but its showing syntax errors. Can some one help me please?
List<String>sample = request.getItemList().forEach(object->{
return sample.add(object.getProductGroup());
});
答案1
得分: 2
以下是翻译好的部分:
使用您现有的代码的工作方式是:
List<String> sample = new ArrayList<>();
request.getItemList().forEach(object -> sample.add(object.getProductGroup()));
但使用 map
操作:
List<String> sample = request.getItemList().stream()
.map(object -> object.getProductGroup())
.collect(Collectors.toList());
英文:
The working way with the code you have is
List<String> sample = new ArrayList<>();
request.getItemList().forEach(object -> sample.add(object.getProductGroup()));
But using map
operation
List<String> sample = request.getItemList().stream()
.map(object -> object.getProductGroup())
.collect(Collectors.tolist());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论