文件:HashSetTest.java
import java.util.*;
public class HashSetTest
{
public static void main(String[] args)
{
HashSet<Student> hs = new HashSet<Student>();
hs.add(new Student("Dreamm",20)); //重复的
hs.add(new Student("张三",20));
hs.add(new Student("Dreamm",20)); //重复的
hs.add(new Student("Dreamming",20));
hs.add(new Student("MY.L",20));
for(Student s : hs)
{
System.out.println("name: " + s.getName());
System.out.println("age: " + s.getAge());
}
System.out.println("长度是:" + hs.size()); //这里为什么会打印5啊?hs中有一对重复的呀,不是应该4吗?
}
}
class Student
{
private String name;
private int age;
Student(String name,int age)
{
this.name = name;
this.age = age;
}
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;
}
}