一:(推荐)
根据Hastset的内部hascode特性 进行判断
- //
- public static void removeDuplicate(ArrayList arlList)
- {
- HashSet h = new HashSet(arlList);
- arlList.clear();
- arlList.addAll(h);
- }
二:
通过Hashset的add方法判断是否已经添加过相同的数据,如果已存在相同的数据则不添加
- 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);
- }