Java Iterator from getSubList(Iterator iterator, int startIndex, int numberOfItems)

Here you can find the source of getSubList(Iterator iterator, int startIndex, int numberOfItems)

Description

Create a list of objects taken from the given iterator and crop the resulting list according to the startIndex and numberOfItems parameters.

License

Artistic License

Parameter

Parameter Description
iterator Iterator
startIndex int starting index
numberOfItems int number of items to keep in the list

Return

List with values taken from the given object, cropped according to startIndex and numberOfItems parameters

Declaration

private static List getSubList(Iterator iterator, int startIndex, int numberOfItems) 

Method Source Code

//package com.java2s;
/**//from  w  w w. j  av a 2 s.c  om
 * Licensed under the Artistic License; you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://displaytag.sourceforge.net/license.html
 *
 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {
    /**
     * Create a list of objects taken from the given iterator and crop the resulting list according to the startIndex
     * and numberOfItems parameters.
     * @param iterator Iterator
     * @param startIndex int starting index
     * @param numberOfItems int number of items to keep in the list
     * @return List with values taken from the given object, cropped according to startIndex and numberOfItems
     * parameters
     */
    private static List getSubList(Iterator iterator, int startIndex, int numberOfItems) {

        List croppedList = new ArrayList(numberOfItems);

        int skippedRecordCount = 0;
        int copiedRecordCount = 0;
        while (iterator.hasNext()) {

            Object object = iterator.next();

            if (++skippedRecordCount <= startIndex) {
                continue;
            }

            croppedList.add(object);

            if ((numberOfItems != 0) && (++copiedRecordCount >= numberOfItems)) {
                break;
            }
        }

        return croppedList;

    }
}

Related

  1. getMaxLength(Iterator i)
  2. getPerplexity(Iterator probStream)
  3. getPersistentKeysIterator(HashMap hashMap)
  4. getSigLinePastOtherAnnotations(String selfCmprurrentLine, Iterator line_itr)
  5. getSomeElements(Iterator iterator, int limit)
  6. getTypedIterator(Iterator it, Class clz)
  7. toIterator(Enumeration enumeration)