The duplicate values in the list collection are processed in most cases by two methods,
One is to assign to another list set after judging by traversing the list collection,
The other is to return to the list collection by assigning a set set.
Method 1:set set to go heavy without disrupting the order
list<string> list =NewArraylist<string>(); List.add ("AAA"); List.add ("BBB"); List.add ("AAA"); List.add ("ABA"); List.add ("AAA");//set set to go heavy without disrupting the orderSet<string> set =NewHashset<string>(); List<String> NewList =NewArraylist<string>(); for(String cd:list) {if(Set.add (CD)) {Newlist.add (CD); }}system.out.println ("Go to the weight of the collection:" + newlist);
Method 2: After traversal, judge to assign to another list collection
// after traversal, judge assigns to another list collection New Arraylist<string>(); for (String cd:list) { if (! Newlist.contains (CD)) { newlist.add (CD);} } System.out.println ("de-weight after collection:" + newlist);
Method 3:set to Weight
// set de-weight New Hashset<string>(); Listnew arraylist<string>(); Set.addall (list); Newlist.addall (set); System.out.println ("de-weight after collection:" + newlist);
Method 4:set (code is reduced to one line)
// set de-weight (reduced to one line) New Arraylist<string> (new hashset<string>(list)); System.out.println ("de-weight after collection:" + newlist);
Method 5: Remove weight and arrange in natural order
// go to weight and arrange in natural order New Arraylist<string> (new treeset<string>(list)); System.out.println ("de-weight after collection:" + newlist);
Article Source: http://blog.csdn.net/cs6704/article/details/50158373
Java ArrayList de-weight