今天偶尔用到去除arraylist中的重复值,在网上找到两种方法,先贴下来,回头研究下
http://dev.rdxx.com/Java/2004-07/26/095859142.shtml
Two Methods to Remove Duplicates in an ArrayList
Here are two methods that allow you to remove duplicates in an ArrayList. removeDuplicate does not maintain the order where as removeDuplicateWithOrder maintains the order with some performance overhead.
两个方法移除arraylist中的重复值
一种方法removeduplicate在移除不维持arraylist原来的顺序
另一种方法removeDuplicateWithOrder维持原来顺序,但消耗运行时间
1.The removeDuplicate Method:
2.The removeDuplicateWithOrder Method:
http://dev.rdxx.com/Java/2004-07/26/095859142.shtml
Two Methods to Remove Duplicates in an ArrayList
Here are two methods that allow you to remove duplicates in an ArrayList. removeDuplicate does not maintain the order where as removeDuplicateWithOrder maintains the order with some performance overhead.
两个方法移除arraylist中的重复值
一种方法removeduplicate在移除不维持arraylist原来的顺序
另一种方法removeDuplicateWithOrder维持原来顺序,但消耗运行时间
1.The removeDuplicate Method:
/** List order not maintained **/
public static void removeDuplicate(ArrayList arlList)
{
HashSet h = new HashSet(arlList);
arlList.clear();
arlList.addAll(h);
}
2.The removeDuplicateWithOrder Method:
/** 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);
}