Java中ArrayList HashSet的使用 以及HashCode的用处(2)

package me.test;
import java.util.*;
public class ReflectTest2
{
   public static void main(String[]args)
   {
    //面向父类的编程或者面向接口编程
    Collection c1=new HashSet() ;
       MyTest t1=new MyTest(2,2)  ;
       MyTest t2=new MyTest(3,4)  ;
       MyTest t3=new MyTest(2,2)  ;
       c1.add(t1) ;
       c1.add(t2) ;
       c1.add(t3) ;
       c1.add(t1) ;
       t1.y=0;
       c1.remove(t1);
       System.out.println(c1.size()); 
   }
  
}
class MyTest
{
 int x;
 int y;
    public  MyTest(int x,int y)
    {
     this.x=x ;
     this.y=y ;
    }
 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + x;
  result = prime * result + y;
  return result;
 }
 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  MyTest other = (MyTest) obj;
  if (x != other.x)
   return false;
  if (y != other.y)
   return false;
  return true;
 }
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wydxfx.html