ImportJava.util.*;/*removes duplicate elements from the ArrayList collection. */classArraylisttest { Public Static voidsop (Object obj) {System.out.println (obj); } Public Static voidMain (string[] args) {ArrayList Al=NewArrayList (); Al.add ("Java01"); Al.add ("Java02"); Al.add ("Java01"); Al.add ("Java02"); Al.add ("Java01");//Al.add ("java03"); /*the next call in the loop in the iteration is hasnext judged once. Iterator it = Al.iterator (); while (It.hasnext ()) {SOP (It.next () + "...." +it.next ()); } */ /**/SOP (AL); Al = Singleelement (AL); SOP (AL); } public static ArrayList singleelement (ArrayList al) {//defines a temporary container. ArrayList Newal = new ArrayList (); Iterator it = Al.iterator (); while (It.hasnext ()) {Object obj = It.next (); if (!newal.contains (obj)) newal.add (obj); } return Newal; }}
The Java collection collection-list-arraylist removes duplicate elements from the ArrayList collection.