Method one: Through the iterator Remove method
Java code
Public voidtestlist () {List<Integer> list=NewArraylist<integer>(); List.add (1); List.add (2); List.add (4); List.add (1); List.add (2); List.add (5); List.add (1); List<Integer> listtemp=NewArraylist<integer>(); Iterator<Integer> it=List.iterator (); while(It.hasnext ()) {intA=It.next (); if(Listtemp.contains (a)) {it.remove (); } Else{Listtemp.add (a); } } for(Integer i:list) {System.out.println (i); } }
Method Two: Assign the result directly to another list
Public voidTestList2 () {List<Integer> list=NewArraylist<integer>(); List.add (1); List.add (2); List.add (4); List.add (1); List.add (2); List.add (5); List.add (1); List<Integer> templist=NewArraylist<integer>(); for(Integer i:list) {if(!templist.contains (i)) {Templist.add (i); } } for(Integer i:templist) {System.out.println (i); } }
Print results
1 2 4
Java List de-weight (two ways)