两种方法删除ArrayList里重复元素
两种方法删除ArrayList里重复元素
JAVA代码技巧
两种方法删除ArrayList里重复元��
- 博客分类:
- java
1.方法一:
- /** List order not maintained **/
- public static void removeDuplicate(ArrayList arlList)
- {
- HashSet h = new HashSet(arlList);
- arlList.clear();
- arlList.addAll(h);
- }
2.方法二:
Java代码 %!(EXTRA markdown.ResourceType=, string=, string=)
- /** List order maintained **/
- public static void removeDuplicateWithOrder(ArrayList arlList)
- {
- Set 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);
- }


