1、通過循環進行刪除
public static void 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);}}}
System.out.println(list);}
2、通過HashSet刪除
public static void removeDuplicate(List list) {
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
System.out.println(list);}
擴展資料:
List集合代表一個元素有序,可重復的集合,集合中每個元素都有對應的順序索引。List接口中增加了一些根據索引操作元素的方法:
void add(int index,E element ) 在列表的指定位置插入該元素。
boolean addAll(int index,Collection c) 將集合c包含的所有元素都插入到List集合的index處。
Object get(int index) 返回集合index索引出的元素。
List額外提供的一個listIterator()方法,提供了專門操作List的方法。
ListIterator接口在Iterator的基礎上增加了如下方法:
boolean hasPrevious(): 返回該迭代器關聯的集合是否還有上一個元素。
Object previous(): 返回該迭代器的上一個元素。
void add((E e): 在指定位置插入一個元素。