private static <T> Stream<List<T>> combinationHelper(
            T[] arr, T[] data,
            int start, int end,
            int index, int r) {

        // Current combination is ready to be printed, print it
        if (index == r) {
            return Stream.of(asList(data, r));
        }

        // replace index with all possible elements. The condition
        // "end-i+1 >= r-index" makes sure that including one element
        // at index will make a combination with remaining elements
        // at remaining positions
        return IntStream.rangeClosed(start, end)
            .filter(i -> end - i + 1 >= r - index)
            .mapToObj(i -> {
                data[index] = arr[i];
                return combinationHelper(arr, data, i + 1, end, index + 1, r);
            }).flatMap(identity());
    }
Creates and returns all possible combinations of the given elements.

The order of the combinations in the stream is unspecified.

@param <T> element type
@param items to combine
@return all possible combinations of the given elements
common-parent/combinatorics/src/main/java/com/speedment/common/combinatorics/internal/CombinationUtil.java