Here you can find the source of trimTail(List
public static <T> void trimTail(List<T> l)
//package com.java2s; //it under the terms of the GNU Affero General Public License as published by import java.util.Collection; import java.util.List; import java.util.ListIterator; public class Main { /**//from w ww .j av a 2 s .c om * Remove all null elements at the end of the list. */ public static <T> void trimTail(List<T> l) { final ListIterator<T> it = l.listIterator(l.size()); while (it.hasPrevious()) { if (it.previous() != null) return; it.remove(); } } /** * @param coll * @postcondition (coll == null) --> (result == 0) * @postcondition (coll != null) --> (result == coll.size()) */ public static int size(Collection<?> coll) { return (coll == null) ? 0 : coll.size(); } }