zoukankan      html  css  js  c++  java
  • Java学习笔记_180717_hashCode问题(不懂)

    package com.hxzy.demo001;
    
    import java.util.HashSet;
    
    class RectObject {
    	public int x;
    	public int y;
    
    	public RectObject(int x, int y) {
    		this.x = x;
    		this.y = y;
    	}
    
    	@Override
    	public int hashCode() {
    		final int prime = 31;
    		int result = 1;
    		result = prime * result + x;
    		result = prime * result + y;
    		return result;
    	}
    
    	@Override
    	public boolean equals(Object obj) {
    		if (this == obj)
    			return true;
    		if (obj == null)
    			return false;
    		if (getClass() != obj.getClass())
    			return false;
    		final RectObject other = (RectObject) obj;
    		if (x != other.x) {
    			return false;
    		}
    		if (y != other.y) {
    			return false;
    		}
    		return true;
    	}
    }
    
    public class Example {
    	public static void main(String[] args) {
    		HashSet<RectObject> set = new HashSet<RectObject>();
    		RectObject r1 = new RectObject(3, 3);
    		RectObject r2 = new RectObject(5, 5);
    		RectObject r3 = new RectObject(3, 6);
    		set.add(r1);
    		set.add(r2);
    		set.add(r3);
    		for (RectObject rectObject : set) {
    			System.out.println(rectObject.hashCode());
    		}
    		r3.y=7;
    		for (RectObject rectObject : set) {
    			System.out.println(r3.equals(rectObject));
    			System.out.println(rectObject.equals(r3));
    		}
    		System.out.println("删除前的大小size:" + set.size());
    		System.out.println(set.contains(r3));
    		System.out.println(set.remove(r3));
    		System.out.println("删除后的大小size:" + set.size());
    	}
    }
    

    根据contains的描述:

    Returns true if this set contains the specified element. More formally, returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)).
    
    Specified by: contains(...) in Set, Overrides: contains(...) in AbstractCollection
    Parameters:
    o element whose presence in this set is to be tested
    Returns:
    true if this set contains the specified element
    

     既然System.out.println(r3.equals(rectObject)); System.out.println(rectObject.equals(r3));返回在rectObject为r3时返回值都为true;为何set.contains(r3)的返回值为false?

    确定是否存在element是根据hashCode确定的,r3.y=7;之后r3的hashcode()返回值改变了,所以set.remove(r3);失败返回值为false;元素个数

    不理解。

    System.out.println("是否包含元素:");
    		for (RectObject rectObject : set) {
    			System.out.println(rectObject.x + ":"+rectObject.y+"--"+set.contains(rectObject));
                    }    
    

     运行这个代码返回值竟然是:true false true;

    看到一种说法,hashcode重写应该保证,同一对象的返回值不改变,暂时只能这么理解了。

    https://blog.csdn.net/violet_echo_0908/article/details/50152915

     hashCode重写原则:

    代码中明显违反了该原则

  • 相关阅读:
    关联分析(Association analysis)
    Django简介
    YAML快速入门
    YAML 在Python中的应用
    抓包工具、日誌抓取、弱網測試
    实现轮询
    Dubbo-从入门到深入
    Spring Boot
    小程序自动生成图片的标签
    小程序上传图片的大坑
  • 原文地址:https://www.cnblogs.com/annofyf/p/9325358.html
Copyright © 2011-2022 走看看