去除一个ArrayList的重复元素有两种方法:(ArrayList与Vector的存储结构是Object[],LinkedList是双向列表)
第一种是不需要借助临时list,用equals方法比较ArrayList中数据,两次遍历原来list;
第二种是借助一个临时ArrayList,向临时List添加数据,调用arrayList.contains(obj)判断是否存在
了解ArrayList的contains原理:调用obj的equals方法进行判断在ArrayList中的位置是否大于零,进而判断是否存在。
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中重复字符串的处理:
@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中重复对象的处理:
Person.java 重写equals方法
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 + "]";
} }
去重算法:
@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]