如何让我的自定义迭代器与每个List一起使用?

Gen*_*zer 0 java generics

我实现了SubListIterator一个小实用程序,Iterator用于迭代给定列表的子列表.

假设我有一个包含13500个元素的List,我想将它拆分为7个子列表并使用它们.

 @Test
    public void shouldSplitTheGivenListIntoSmallerLists() {
        List<Long> given = new ArrayList<Long>();
        for (int count = 0; count < 13500; count++) {
            given.add(Long.valueOf(count));
        }

        List<List<Long>> actualSubLists = new ArrayList<List<Long>>();
        for (List<Long> subList : SubListIterator.subList(given, 2000)) { // Line got compilation error
            actualSubLists.add(subList);
        }

        assertEquals(7, actualSubLists.size());
    }
Run Code Online (Sandbox Code Playgroud)

如果我SubListIterator直接实现,一切都很好List<Long>.

然后我想延长我的SubListIterator每工作List,无论他们的泛型类型,所以我去改变List<Long>,以List<?>并获得编译错误:

Type mismatch: cannot convert from element type List<?> to List<Long>

我尝试过List<T>,它也不起作用.

我的问题是:无论如何,实现我的目标是让SubListIterator每个人都与之合作List,而不仅仅是List<Long>

以下是SubListIterator:

public class SubListIterator implements Iterator<List<?>>, Iterable<List<?>> {

    public static SubListIterator subList(List<?> given, int itemsEachSubList) {
        return new SubListIterator(given, itemsEachSubList);
    }

    private final List<?> whole;
    private final int elementsEachPart;
    private int fromIndex;
    private int toIndex;

    public SubListIterator(List<?> whole, int itemsEach) {
        this.whole = whole;
        this.elementsEachPart = itemsEach;
        this.fromIndex = 0;
        this.toIndex = elementsEachPart;
    }

    @Override
    public boolean hasNext() {
        return fromIndex < toIndex;
    }

    @Override
    public List<?> next() {
        List<?> nextSubList = whole.subList(fromIndex, toIndex);
        fromIndex = toIndex;
        toIndex = Math.min(toIndex + elementsEachPart, whole.size());
        return nextSubList;
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("This method is not supported");
    }

    @Override
    public Iterator<List<?>> iterator() {
        return this;
    }
}
Run Code Online (Sandbox Code Playgroud)

感谢您的支持

小智 6

您需要概括SubListIterator.

public class SubListIterator<T> implements Iterator<List<T>>, Iterable<List<T>> {

    public static <C> SubListIterator<C> subList(List<C> given, int itemsEachSubList) {
        return new SubListIterator<C>(given, itemsEachSubList);
    }

    private final List<T> whole;
    private final int elementsEachPart;
    private int fromIndex;
    private int toIndex;

    public SubListIterator(List<T> whole, int itemsEach) {
        this.whole = whole;
        this.elementsEachPart = itemsEach;
        this.fromIndex = 0;
        this.toIndex = elementsEachPart;
    }

    @Override
    public boolean hasNext() {
        return fromIndex < toIndex;
    }

    @Override
    public List<T> next() {
        List<T> nextSubList = whole.subList(fromIndex, toIndex);
        fromIndex = toIndex;
        toIndex = Math.min(toIndex + elementsEachPart, whole.size());
        return nextSubList;
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("This method is not supported");
    }

    @Override
    public Iterator<List<T>> iterator() {
        return this;
    }

}
Run Code Online (Sandbox Code Playgroud)