Replace for loop with java 8 foreach for updating values

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

I'm looking to replace the following for loop with an elegant java 8 stream or lambda solution. Is there anything concise and efficient?

    public static void main(String[] args) {         ArrayList<Integer> myList = new ArrayList<>( Arrays.asList( 10,-3,5));          // add 1/2 of previous element to each element         for(int i =1 ;i < myList.size();  ++i )             myList.set(i, myList.get(i)+myList.get(i-1)/2);          // myList.skip(1).forEach( e -> e + prevE/2 );  // looking for something in this spirit     } 

回答1:

Your loop evaluation has a dependency to the result of the previous evaluation. It is equivalent to

for(int i = 1, value = myList.get(0); i < myList.size(); i++ ) {     value = myList.get(i) + value/2;     myList.set(i, value); } 

There is no real simplification by using the Stream API or lambda expressions possible. In fact, I would prefer the variant shown above, even if it’s bigger rather than smaller, as it makes clear what actually happens (and may be slightly more efficient by avoiding multiple List lookups).

It also allows you to program position independent, if you create a new List:

List<Integer> srcList = Arrays.asList(10, -3, 5), dstList = new ArrayList<>();  int value = 0; for(Integer i: srcList) {     value = i + value/2;     dstList.add(value); } 


回答2:

There is no way to access previous element in the java stream. You might use IntStream#range but it doesn't look elegant:

IntStream.range(1, myList.size())         .forEachOrdered(i -> myList.set(i, myList.get(i) + myList.get(i - 1) / 2)); 


回答3:

Yes you can achieve this using this line of code:

IntStream.range(1,myList.size())     .forEachOrdered(i->myList.set(i,(myList.get(i)+myList.get(i-1)/2))); System.out.println(myList); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!