Here are two methods that allow you to remove duplicates in an ArrayList removeDuplicate does not maintain the order where as removeDuplicateWithOrder maintains the order with some performance overhead The removeDuplicate Method: /** List order not maintained **/ public static void removeDuplicate(ArrayList arlList) { HashSet h = new HashSet(arlList); arlListclear(); arlListaddAll(h); } The removeDuplicateWithOrder Method: /** List order maintained **/ public static void removeDuplicateWithOrder(ArrayList arlList) { Set set = new HashSet(); List newList = new ArrayList(); for (Iterator iter = erator(); iterhasNext(); ) { Object element = iternext(); if (setadd(element)) newListadd(element); } arlListclear(); arlListaddAll(newList); } |