java 13 hashset 如果有即添加不进去

package com.java13.myjava;

import java.util.HashSet;
import java.util.Set;

import org.junit.Test;

public class Hashcodetest {
@Test
public void testhash(){
Set<Dog> set=new HashSet<Dog>();

Dog d =new Dog(20,40);
//如果有就的添加不进去
set.add(d);
set.add(d);
set.add(new Dog(20,40));
System.out.println(set.size());
}
}

class Dog{
int x=1;
public int weight;
public int height;
//alt+shift+S 快捷生成构造函数

public Dog(){
super();
}

public Dog(int weight, int height){
super();
this.height=height;
this.weight=weight;
}

public int hashCode(){
return height+weight;
}
public boolean equals(Object obj){
return false;
}
}

猜你喜欢

转载自www.cnblogs.com/simly/p/11040019.html