Returns a truncated version of the specified List. - Java java.util

Java examples for java.util:List Operation

Description

Returns a truncated version of the specified List.

Demo Code


//package com.java2s;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] argv) {
        List list = java.util.Arrays.asList("asdf", "java2s.com");
        int len = 42;
        System.out.println(truncateList(list, len));
    }//from w w w. j  a  va 2  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 List truncateList(List list, int len) {
        if (len >= list.size())
            return list;
        ArrayList newList = new ArrayList(len);
        for (int ii = 0; ii < len; ii++) {
            newList.add(list.get(ii));
        }
        return newList;
    }
}

Related Tutorials