英文:
I want to convert the below code to a lambda expression so that i can remove ugly for loops
问题
我想将下面的代码转换为Lambda表达式:
IntStream.range(0, commentsCount)
.forEach(i -> System.out.println(js1.getInt("fields.comment.comments[" + i + "].id")));
英文:
I want to convert the below code to a lambda expression
for (int i=0; i < commentsCount ;i++) {
System.out.println(js1.getInt("fields.comment.comments[" + i + "].id"));
}
答案1
得分: 3
你可以尝试在这里使用IntStream
,以及一个lambda表达式来构建每个要打印的字符串表达式:
IntStream.range(0, commentsCount)
.map(i -> js1.getInt("fields.comment.comments[" + i + "].id"))
.forEach(System.out::println);
英文:
You could try using an IntStream
here along with a lambda to build each string expression to be printed:
IntStream.range(0, commentsCount)
.map(i -> js1.getInt("fields.comment.comments[" + i + "].id"))
.forEach(System.out::println);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论