去除一個ArrayList的重複元素有兩種方法:(ArrayList與Vector的存儲結構是Object[],LinkedList是雙向列表)java
第一種是不須要藉助臨時list,用equals方法比較ArrayList中數據,兩次遍歷原來list;算法
第二種是藉助一個臨時ArrayList,向臨時List添加數據,調用arrayList.contains(obj)判斷是否存在ide
瞭解ArrayList的contains原理:調用obj的equals方法進行判斷在ArrayList中的位置是否大於零,進而判斷是否存在。this
public boolean contains(Object o) { return indexOf(o) >= 0; } /** * Returns the index of the first occurrence of the specified element * in this list, or -1 if this list does not contain the element. * More formally, returns the lowest index <tt>i</tt> such that * <tt>(o==null ? get(i)==null : o.equals(get(i)))</tt>, * or -1 if there is no such index. */ public int indexOf(Object o) { if (o == null) { for (int i = 0; i < size; i++) if (elementData[i]==null) return i; } else { for (int i = 0; i < size; i++) if (o.equals(elementData[i])) return i; } return -1; }
1. 兩種方式對ArrayList中重複字符串的處理:
spa
@Test // 第一種去重複方法 public void Test1() { List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add("4"); list.add("3"); list.add("2"); list.add("1"); System.out.println("去重複元素以前:"); for (String s : list) { System.out.println(s); } // 遍歷ArrayList for (int i = 0; i < list.size() - 1; i++) { for (int j = i + 1; j < list.size(); j++) { if (list.get(i).equals(list.get(j))) { list.remove(j); } } } System.out.println(list.size()); System.out.println("去重複元素以後:"); for (String s : list) { System.out.println(s); } } @Test // 第二種去重複方法 public void Test2() { List<String> list = new ArrayList<String>(); list.add("1"); list.add("2"); list.add("3"); list.add("4"); list.add("4"); list.add("3"); list.add("2"); list.add("1"); List<String> temp = new ArrayList<String>(); Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { String str = iterator.next(); if (!temp.contains(str)) { temp.add(str); } } for (String s : temp) { System.out.println(s); } }
2. 兩種方式對ArrayList中重複對象的處理:rest
Person.java 重寫equals方法code
package cn.xm.exam.test.javaTest; import static org.hamcrest.CoreMatchers.instanceOf; public class Person { private String name; private String id; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public Person(String name, String id) { super(); this.name = name; this.id = id; } // 重寫一個Bean的hashCode方法 /* * @Override public int hashCode() { // TODO Auto-generated method stub * return this.name.hashCode()+this.id.hashCode(); } */ @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Person)) { throw new ClassCastException();// 類型錯誤 } Person pp = (Person) obj; return this.getId().equals(pp.getId()) && this.getName().equals(pp.getName()); } @Override public String toString() { return "Person [name=" + name + ", id=" + id + "]"; } }
去重算法:orm
@Test // 第一種去重複方法去除Object重複的 public void Test3() { List<Person> list = new ArrayList<Person>(); list.add(new Person("張三", "1")); list.add(new Person("張三", "2")); list.add(new Person("張三", "3")); list.add(new Person("張三", "4")); list.add(new Person("李四", "1")); list.add(new Person("李四", "2")); list.add(new Person("張三", "1")); list.add(new Person("李四", "1")); // 遍歷ArrayList for (int i = 0; i < list.size() - 1; i++) { for (int j = i + 1; j < list.size(); j++) { if (list.get(i).equals(list.get(j))) { list.remove(j); } } } for (Person p : list) { System.out.println(p); } } @Test // 第二種去重複方法去除Object重複的 public void Test4() { List<Person> list = new ArrayList<Person>(); list.add(new Person("張三", "1")); list.add(new Person("張三", "2")); list.add(new Person("張三", "3")); list.add(new Person("張三", "4")); list.add(new Person("李四", "1")); list.add(new Person("李四", "2")); list.add(new Person("張三", "1")); list.add(new Person("李四", "1")); List<Person> temp = new ArrayList<Person>(); Iterator<Person> iterator = list.iterator(); while (iterator.hasNext()) { Person person = iterator.next(); if (!temp.contains(person)) { temp.add(person); } } for (Person p : temp) { System.out.println(p); } }
結果:對象
Person [name=張三, id=1] Person [name=張三, id=2] Person [name=張三, id=3] Person [name=張三, id=4] Person [name=李四, id=1] Person [name=李四, id=2]