Java List Truncate truncateList(List list, int len)

Here you can find the source of truncateList(List list, int len)

Description

Returns a truncated version of the specified List.

License

Apache License

Parameter

Parameter Description
list the <code>List</code>
len the truncate length

Return

the truncated List

Declaration

public static <T> List<T> truncateList(List<T> list, int len) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    /**/*from  www.j  a  v  a2  s.  c om*/
     * Returns a truncated version of the specified <code>List</code>.
     * @param list the <code>List</code>
     * @param len the truncate length
     * @return the truncated <code>List</code>
     */
    public static <T> List<T> truncateList(List<T> list, int len) {
        if (len >= list.size())
            return list;
        ArrayList<T> newList = new ArrayList<T>(len);
        for (int ii = 0; ii < len; ii++) {
            newList.add(list.get(ii));
        }
        return newList;
    }
}

Related

  1. truncate(List list, int length)
  2. truncateEnd(List list, int numElements)
  3. truncateList(final List full, int maxSize)
  4. truncateList(int offset, int count, List originalList)
  5. truncateList(List input, int maxSize)
  6. truncateStringList(List strings, String truncateFrom)
  7. truncateVersionFromModFileName(List fileNames)