Assuming this is an existing list that you want to modify:
public static void truncate20(List<String> strs) { for (ListIterator<String> iter = strs.listIterator(); iter.hasNext();) { String str = iter.next(); if (str.length() > 20) { iter.set(str.substring(0, 20)); } } }
Creating a new list is simple enough - just add each iteration.
If you want to do this on the insert, I suggest you write a class for it that * does not extend List . Just give the operation you need that makes sense.
source share