题目讲解:
我们现在要将一个集合内添加的学生类对象中,将具有相同成员变量中的对象去掉。如:
Student s1 = new Student(10, "1");
Student s2 = new Student(10, "1");
s1与s1具有相同的成员变量值在集合中就应该保留其中一个对象。
学生类代码
package test;
public class Student {
private int age;
private String name;
public Student(int age, String name) {
super();
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student [age=" + age + ", name=" + name + "]\n";
}
}
操作集合的代码
package test;
import java.util.ArrayList;
public class Demo1 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建集合
ArrayList list = new ArrayList();
ArrayList list1 = new ArrayList();
//创建引用类型对象
Student s1 = new Student(10, "w1");
Student s2 = new Student(10, "w2");
Student s3 = new Student(10, "w1");
Student s4 = new Student(10, "w4");
Student s5 = new Student(10, "w9");
//在集合中添加引用类型变量
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
//通过建立2个不同的集合,只要向空的集合中加入不重复的值即可
for(int i = 0;i<list.size();i++){
Student s = (Student)list.get(i);
if(!list1.contains(s)){//判断list1集合中是否含有list的重复值
list1.add(s);
}
}
//输出没有重复值的集合
System.out.println("list1"+list1);
}
}
结果:
list1[Student [age=10, name=w1]
, Student [age=10, name=w2]
, Student [age=10, name=w1]
, Student [age=10, name=w4]
, Student [age=10, name=w9]
]
查看结果发现并没有实现预想的效果
于是我查了contains的源码
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
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;
}
讲解
看到与equals有关然后用想到了引用类型的equals()方法是没有被重写也即是继承了Object的equals()方法。之后就想明白了Object类的equals方法比较的是栈值(地址值)而我们需要比较的是成员变量的值。
关于Object的equals()方法具体请看https://blog.csdn.net/qq_35654259/article/details/84777858
解决
只要将equals()方法重写改为比较成员变量值就行 只要在eclipse的Student.java文件上按住"ctrl+shift+s"之后点击
Generate hasCode() and equals()就行。修改后Student代码如下Demo1代码不需要改变。
package day16;
public class Student {
private int age;
private String name;
public Student(int age, String name) {
super();
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (age != other.age)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Student [age=" + age + ", name=" + name + "]\n";
}
}