1. 循環list中的全部元素而後刪除重複
java
public static List removeDuplicate (List list){
for (int i = 0; i < list.size() - 1; i++) {
for (int j = list.size() - 1; j > i; j--) {
if (list.get(j).equals(list.get(i))) {
list.remove(j);
}
}
}
return list;
}
spa
2. 經過HashSet踢除重複元素
public static List removeDuplicate(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
return list;
}.net
在groovy中固然也可使用上面的兩種方法, 但groovy本身提供了unique方法來去除重複數據
blog
def list = [1, 2, 3, 2, 4, 1, 5]
list.unique() // [1, 2, 3, 4, 5]
rem