- package com.itheima;
- import java.io.*;
- import java.util.*;
- public class ReflectTest4 {
- public static void main(String args[]) throws Exception
- {
- Collection collections = new HashSet();
- ReflectPerson r1 = new ReflectPerson(3,3);
- ReflectPerson r2 = new ReflectPerson(4,4);
- ReflectPerson r3 = new ReflectPerson(5,5);
- ReflectPerson r4 = new ReflectPerson(3,3);
- collections.add(r1);
- collections.add(r2);
- collections.add(r3);
- collections.add(r4);
- System.out.println(r1.hashCode()==r4.hashCode());
- System.out.println(r1.equals(r4));
- System.out.println(collections.size());
- }
- }
复制代码 代码如图。运行结果为:
true
true
4
为什么r1与r4的hashcode与equals都相等还存入了HashSet中。
|
|