ForEachUtils


import java.util.Objects;
import java.util.function.BiConsumer;

public class ForEachUtils {
    /**
     *
     * @param <T>
     * @param startIndex 開始遍歷的索引
     * @param elements 集合
     * @param action
     */
    public static <T> void forEach(int startIndex,Iterable<? extends T> elements, BiConsumer<Integer, ? super T> action) {
        Objects.requireNonNull(elements);
        Objects.requireNonNull(action);
        if(startIndex < 0) {
            startIndex = 0;
        }
        int index = 0;
        for (T element : elements) {
            index++;
            if(index <= startIndex) {
                continue;
            }

            action.accept(index-1, element);
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章