• 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重写原则:

    代码中明显违反了该原则

  • 相关阅读:
    linux如何给程序添加自启动
    nginx 反向代理apache服务器 配置java与PHP共存环境
    eclipse配置Js环境spket
    Linux下实现秒级定时任务的两种方案
    Linux时间戳和标准时间的互转
    thinkphp与php共享session
    安装PHP sphinx扩展 sphinx-1.1.0/sphinx.c:105:2: error: too few arguments 错误
    MySQLCouldn't find MySQL manager
    PHP 使用header函数设置HTTP头的示例方法 表头 (xlsx下载)
    JAVA正则表达式 Pattern和Matcher
  • 原文地址:https://www.cnblogs.com/annofyf/p/9325358.html
Copyright © 2020-2023  润新知