我想将下面的代码转换为Lambda表达式,以便可以去除繁琐的for循环。

huangapple go评论68阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年9月6日 11:23:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63760425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定