用List去除重复元素
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class LinkedListTest {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Student("李四", 21));
list.add(new Student("张三", 20));
list.add(new Student("王二", 23));
list.add(new Student("小黄", 24));
list.add(new Student("李四", 21));
getSingleElement(list);
}
public static void getSingleElement(List list) {
// 临时容器 存放唯一性元素
List temp = new ArrayList();
// 遍历原容器
for (Iterator it = list.iterator(); it.hasNext();) {
Object obj = (Object) it.next();
// 判断是否存在,如果不存在则将所读取的元素传入临时容器中
if (!temp.contains(obj)) {
temp.add(obj);
}
}
// 唯一性的元素已经被记录到临时容器中
// 清空原容器中的元素
list.clear();
System.out.println(list);
// 把临时容器中的元素添加到原容器中
list.addAll(temp);
System.out.println(list);
}
}
package ch;
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public boolean equals(Object obj) {
System.out.println("判断equlas被调用次数,可说明重复次数");
if(this == obj){
return true;
}
if(!(obj instanceof Student)){
return false;
}
Student stu = (Student)obj;
return this.name.equals(stu.name) && this.age == stu.age;
}
}
用Set接口去除重复元素
*set接口的特点:不包含重复的元素集合
*但是对于自定义对象仍包含有重复对象,则需要用HashSet();
*HashSet:哈希表结构,保证元素唯一性的方式要依赖于hashCode方 法和equals方法。
*哈希算法也称为散列算法,所得结果称哈希值
*优:存储的元素是通过哈希值对应关系,该数组为哈希表,查询
速度比数组要快。
*劣:重复元素无法储存,因为要保证元素的唯一性
*当哈希值相同时(hashCode()一致),发生哈希冲突,再次判断元素的内容是否相同(equals())
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class SetDemo {
public static void main(String[] args) {
Set set = new HashSet();
set.add(new Student("小黄", 24));
set.add(new Student("李四", 21));
set.add(new Student("张三", 20));
set.add(new Student("王二", 23));
set.add(new Student("小黄", 24));
set.add(new Student("李四", 21));
for (Iterator iterator = set.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
System.out.println(object);
}
}
}
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
@Override
public boolean equals(Object obj) {
System.out.println("判断equlas被调用次数,可说明重复次数");
if (this == obj) {
return true;
}
if (!(obj instanceof Student)) {
return false;
}
Student stu = (Student) obj;
return this.name.equals(stu.name) && this.age == stu.age;
}
@Override
public int hashCode() {
return name.hashCode() + age;
}
}
小结:对于自定义对象中有重复对象时,需要重新HashCode和equals方法。