Java - Iterating over every two elements in a list

前端 未结 11 2130
梦毁少年i
梦毁少年i 2020-12-19 04:26

What\'s the best way to iterate over a list while processing 2 elements at the same time?

Example:

List strings = Arrays.asList(\"item          


        
11条回答
  •  星月不相逢
    2020-12-19 05:24

    Now in Java 8, by using https://github.com/wapatesh/fig

    You can write:

    seq.forEachSlice(2, (values)->{
        // [1,2]  [3, 4]
    });
    

提交回复
热议问题