利用 Set 去掉 list中重复值
1.对原来list顺序不要求
public static void removeDuplicate(ArrayList arlList)
{
HashSet h = new HashSet(arlList);
arlList.clear();
arlList.addAll(h);
}
2.对原来list顺序不变
public static void removeDuplicateWithOrder(ArrayListarlList)
{
Set set = newHashSet();
List newList = newArrayList();
for (Iterator iter = arlList.iterator(); iter.hasNext();)
{
Object element =iter.next();
if (set.add(element))newList.add(element);
}
arlList.clear();
arlList.addAll(newList);
}
这里主要用了set add API 的作用当有了重复值时,返回false
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。