---
title: "equals and hashcode "
author: "Roader123"
date: "2011-09-10T18:30:44Z"
tags: []
categories: ["编程语言"]
slug: "equals-and-hashcode"
draft: false
lastmod: "2011-09-10T18:30:44Z"
id: 1169896
url: "https://roader123.iteye.com/blog/1169896"
---

一般重写equals, 需要重写hashcode。for hash Collection usage

> 
> 	public boolean equals(Object o){
> 		if(o==this)
> 			return true;
> 		if(!(o instanceof CatchFinally))
> 			return false;
> 		CatchFinally cf = (CatchFinally)o;		
> 		return cf.keyfield == this.keyfield&&this.keyfield2.equals(cf.keyfield2);
> 	}
> 	
> 	public int hashCode(){
> 		int result =17;
> 		result = 37*result + keyfield;
> 		result = 37*result + keyfield2;
> 		return result;
> 	}
>