zoukankan      html  css  js  c++  java
  • 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;
    }
    }

  • 相关阅读:
    C#
    C#
    C#
    python——socket网络编程
    Python——面向对象
    Python——函数
    Python——列表深浅拷贝
    Python——文件操作
    多级菜单(增强版)
    Python 编码机制
  • 原文地址:https://www.cnblogs.com/simly/p/11040019.html
Copyright © 2011-2022 走看看