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

时间:2014-09-25 06:29:00

标签: 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());
    }

如果我直接使用SubListIterator实现List<Long>,那么一切正常。

然后我想扩展我的SubListIterator以适应每个List,无论其通用类型如何,所以我将List<Long>更改为List<?>并获得编译错误:< / p>

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

我尝试使用List<T>并且它也无法正常工作。

我的问题是:无论如何要实现我的目标,即使SubListIteratorList合作,而不只是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;
    }
}

感谢您的支持

1 个答案:

答案 0 :(得分: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;
    }

}