zoukankan      html  css  js  c++  java
  • ==,hashcde, equals(一)

    1、Hash  的属性, 

        1)bucket 和 list

    2、java.lang.object 的 hashcode 和 equal 通过内存地址比较

    3、为什么要重写hashcode 和 equals, 

         1) 实现hashset不可重复的特性(http://www.cnblogs.com/happyPawpaw/p/3744971.html)

          2)Hashmap 

    4、Hashmap的键值如果是类

    5、Hashmap的put 和 get 方法

        https://blog.csdn.net/lan12334321234/article/details/70048493   

        (https://blog.csdn.net/VIP_WangSai/article/details/77505517)

    6、先判断hashcode, 再判断equals 

         为什么要这种判断顺序呢,因为要实现的是hash的特性,bucket由hashcode 决定;

          hashcode  true, equal 不一定 true

          equals true, hashcode 一定true

    ==,hashcde, equals

    public class Student{
        private int id;
        private String name;
        
        public Student(int id, String name){
            this.id = id;
            this.name = name;
        }    
    }
    

      

    main

    public static void main(String args[]) { 
            
            Student student01 = new Student(1,"lee");
            Student student02 = new Student(1,"lee");
            
            System.out.print("student01 == student02 :");
            if(student01 == student02){
                System.out.println("true.");
            }else{
                System.out.println("false.");
            }
            
            System.out.print("student01.hashCode() == student02.hashCode() :");
            if(student01.hashCode() == student02.hashCode()){
                System.out.println("true.");
            }else{
                System.out.println("false.");
            }
            
            System.out.print("student01.equals(student02) :");
            if(student01.equals(student02)){
                System.out.println("true.");
            }else{
                System.out.println("false.");
            }
            
             Set<Object> set = new HashSet<Object>();
             set.add(student01);
             set.add(student02);
             System.out.println("HashSet 长度:" + set.size());
        }

    输出

    默认,继承Object
    student01 == student02 :false.
    student01.hashCode() == student02.hashCode() :false.
    student01.equals(student02) :false.
    HashSet 长度:2  

    修改student 的 hashcode类如下

    public class StudentModifyHashCode{
    	
    	private int id;
    	private String name;
    	
    	public StudentModifyHashCode(int id, String name){
    		this.id = id;
    		this.name = name;
    	}
    	
    	@Override
    	public int hashCode(){
    		final int prime = 31;
    		int result = 1;
    		result = prime*result+this.id;
    		return result;
    	}
    }
    

      

    输出,可以看到这次hashcode 一样的

    修改hashcode
    student01 == student02 :false.
    student01.hashCode() == student02.hashCode() :true.
    student01.equals(student02) :false.
    HashSet 长度:2
    

      

    同时修改student的hashcode 和 equals 方法如下

    public class StudentModifyEqualAndHash{
        
        private int id;
        private String name;
        
        public StudentModifyEqualAndHash(int id, String name){
            this.id = id;
            this.name = name;
        }
        
        public int getId() {
            return id;
        }
    
        public String getName() {
            return name;
        }
    
        @Override
        public int hashCode(){
            final int prime = 31;
            int result = 1;
            result = prime*result+this.id;
            return result;
        }
        
        @Override
        public boolean equals(Object obj){
            if(this == obj)
                return true;
            if(obj == null)
                return false;
            if(getClass() != obj.getClass())
                return false;
            StudentModifyEqualAndHash other = (StudentModifyEqualAndHash)obj;        
            if(this.id != other.getId() ){         
                return false;
            } 
            for(int i=0;i<this.name.length();i++){    
                    if(this.name.charAt(i) != (((StudentModifyEqualAndHash)obj).getName().charAt(i))){    
                          return false;
                    }        
            }
            return true;
        }        
    }
    

      

    输出,可以看到hashcode 和 equals 都一样

    修改hashcode和equals
    student01 == student02 :false.
    student01.hashCode() == student02.hashCode() :true.
    student01.equals(student02) :true.
    HashSet 长度:1

     https://blog.csdn.net/lixiaoxiong55/article/details/93376852

     hashCode作用: https://blog.csdn.net/qq_38977097/article/details/80834525

     

  • 相关阅读:
    【Java基础】2、Java中普通代码块,构造代码块,静态代码块区别及代码示例
    【Java基础】1、java中的instanceof
    【Java面试】1、基础知识篇
    【github&&git】3、git图像化界面GUI的使用
    【github&&git】2、github入门到上传本地项目
    Spring Boot属性配置文件详解
    微服务Spring Cloud与Kubernetes比较
    导入时如何定制spring-boot依赖项的版本
    spring容器ApplicationContext初始化(spring应用上下文初始化)
    spring概念简介、bean扫描与注册实现方式
  • 原文地址:https://www.cnblogs.com/Jomini/p/9638254.html
Copyright © 2011-2022 走看看