- import java.util.*;
- class Student{
- String id;
- String name;
- public Student(String id,String name) {
- this.id=id;
- this.name=name;
- }
- public String toString() {
- return id+":"+name;
- }
- }
- public class Example {
- public static void main(String[]args) {
- HashSet hset=new HashSet();
- hset.add(new Student("1","张三"));
- hset.add(new Student("2","李四"));
- hset.add(new Student("2","李四"));
- System.out.println(hset);
- }
- }
运行结果:
[1:张三, 2:李四, 2:李四]
注:没有去掉重复元素,因为在Student类中没有重写hashCode()和equals()方法。