zoukankan      html  css  js  c++  java
  • java中equals和hashcode方法的重载

    这是一个重载过的类:

    class People {
    private String name;

    public People(String name) {
       this.name = name;
    }

    public String getName() {
       return name;
    }
    public boolean equals(Object object) {
       if (this == object) {
        return true;
       }
       if (object instanceof People) {
        People people = (People) object;
        if (name.equals(people.getName())) {
         return true;
        }
       }
       return false;
    }

    public int hashCode() {
       return name.hashCode();
    }

    }

    下面是测试类:

    public class SetTest {

    public static void main(String[] args) {
        Set<People> set = new HashSet<People>();
        People p1 = new People("zhangsan");
        People p2 = new People("lisi");
        People p3 = new People("zhangsan");
        set.add(p1);
        set.add(p2);
        set.add(p3);
        System.out.println(p1);
        System.out.println(p2);
        System.out.println(p3);
       
        for(Iterator<People> iter = set.iterator(); iter.hasNext();) {
         System.out.println(iter.next().getName());
        }
      
        System.out.println("test".hashCode());
        System.out.println("test1".hashCode());

      
    }

    }

  • 相关阅读:
    安装Kudu
    flume+kafka+spark streaming整合
    安装Kafka
    DataFrame格式化
    RDD/Dataset/DataFrame互转
    多个jar包合并成一个jar包的办法
    flume使用示例
    ecplise + hadoop 调试环境搭建
    web.xml文件加载顺序
    Web.xml配置参数详解
  • 原文地址:https://www.cnblogs.com/xinzhuangzi/p/4100638.html
Copyright © 2011-2022 走看看