Memoryizz

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;
}
}

posted on 2019-06-17 15:38  Memoryizz  阅读(300)  评论(0编辑  收藏  举报

导航