@FunctionalInterface public interface IndexedConsumer<T> { // Return true to continue, false to break boolean accept(int index, T item); } public static <T> void forEachIndexed(List<T> list, IndexedConsumer<T> action) { for (int i = 0; i < list.size(); i++) { if (!action.accept(i, list.get(i))) { break; } } } // USAGE EXAMPLE: List myList = Arrays.asList("Apple", "Banana", "Strawberry"); forEachIndexed(myList, (index, value) -> { System.out.println("Index: " + index + ", Value: " + value); return true; });