There are two ways to delete repeated elements in an arraylist. In the following program segment, the removeduplicate method does not maintain the order, while the removeduplicatewithorder method maintains the order, but it may sacrifice performance.
The removeduplicate method:
/** List order not maintained **/public static void removeduplicate (arraylist arllist) {hashset H = new hashset (arllist); arllist. clear (); arllist. addall (h );}
The removeduplicatewithorder method:
/** List order maintained **/public static void removeduplicatewithorder (arraylist arllist) {set = new hashset (); List newlist = new arraylist (); for (iterator iter = arllist. iterator (); ITER. hasnext ();) {object element = ITER. next (); If (set. add (element) newlist. add (element);} arllist. clear (); arllist. addall (newlist );}
Two ways to delete repeated elements in arraylist