Java Utililty Methods List Truncate

List of utility methods to do List Truncate

Description

The list of methods to do List Truncate are organized into topic(s).

Method

LsetSize(L list, int newsize)
Truncates 'list' or adds null elements to its end
int n = list.size();
if (newsize > n) {
    list.addAll(java.util.Collections.<T>nCopies(newsize - n, null));
} else if (newsize < n) {
    list.subList(newsize, n).clear();
return list;
voidsetSize(List list, int size)
set Size
if (size < list.size()) {
    list.subList(size, list.size()).clear();
} else {
    while (size > list.size()) {
        list.add(null);
Listtrunc(List as, int length)
trunc
if (length < as.size())
    remove(as, length, as.size());
return as;
voidtruncate(final List list, final int newSize)
Truncate a list to a size by removing elements at its end, if necessary.
final int currentSize = list.size();
for (int i = currentSize - 1; i >= newSize; i--) {
    list.remove(i);
Listtruncate(final List items, final int limit)
truncate
if (limit > items.size()) {
    return new ArrayList<>(items);
final List<T> truncated = new ArrayList<>(limit);
for (final T item : items) {
    truncated.add(item);
    if (truncated.size() == limit) {
        break;
...
voidtruncate(List list, int length)
truncate
while (list.size() > length) {
    list.remove(list.size() - 1);
voidtruncateEnd(List list, int numElements)
Truncates the given list by removing elements from the end such that numElements entries remain.
if (list.size() > numElements) {
    list.subList(numElements, list.size()).clear();
ListtruncateList(final List full, int maxSize)
Truncates the given list to have the given maximum size.
final int numToDisplay = Math.min(full.size(), maxSize);
return full.subList(0, numToDisplay);
voidtruncateList(int offset, int count, List originalList)
truncate List
List<T> retVal = new ArrayList<T>(originalList);
if (offset > 0) {
    if (offset < retVal.size()) {
        retVal.subList(0, offset).clear();
if (count > 0 && count < retVal.size()) {
    retVal.subList(count, retVal.size()).clear();
...
ArrayListtruncateList(List input, int maxSize)
truncate List
if (input == null) {
    input = new ArrayList<T>();
int endIndex = Math.min(input.size(), maxSize);
ArrayList<T> result = new ArrayList<T>(endIndex);
for (int i = 0; i < endIndex; i++) {
    result.add(input.get(i));
return result;