-
2006-08-01
Remove Duplicates from a List
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
the best way to remove duplicates from a List in Java. This isn’t too tough to code up yourself (and there are a lot of examples on the net), but Java’s Collection classes actually make this a pretty easy task. Just add all the items in the list to a LinkedHashSet. Because it’s a set, duplicates won’t be added to the collection. Because it’s backed by a linked list, item order will be preserved. And because it uses a hashmap, checking to see if an item is already in the list should be pretty quick (near constant time).
http://henterji.blogbus.com/logs/2951575.html
Here’s the code:public List removeDuplicates(List items) {
Set set = new LinkedHashSet();
set.addAll(items);
return new ArrayList(set);
}随机文章:
Java swing tutorial Website 2006-09-29How to encode Enums? 2006-08-08计算Java日期 2006-08-02Swing and Roundabouts 4: Grid Bag Grease 2006-07-28
收藏到:Del.icio.us







