java中判断类对象是否相等的方法
2014-08-09 08:17
501 查看
==操作符判断的是地址是否相等,equals()判断的是内容是否相等。判断类是否相等要重写equals()方法,
一种方法是用hashcode()和equals()方法,另一种方法用instanceof()
instanceof 是保留关键字,作用是判断左边的对象是否是右边类的实例。
instanceof方法:
主函数:
结果是:
hashCode()和equals()方法:
public class Birds {
private int weight;
private String name;
public Birds(){
this.weight = 140;
this.name = "huanghe";
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int hashCode(Object obj) {
int result = 1;
int prime = 31;
Birds b = (Birds) obj;
result = result * prime + weight;
result = result * prime + ((b.name.equals(name))?0:(b.name).hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
Birds b = (Birds) obj;
if (weight != b.weight) return false;
if (name != b.name) return false;
return true;
}
}
结果:
true
false
一种方法是用hashcode()和equals()方法,另一种方法用instanceof()
instanceof 是保留关键字,作用是判断左边的对象是否是右边类的实例。
instanceof方法:
public class Birds { private int weight; private String name; public Birds(){ this.weight = 140; this.name = "huanghe"; } public int getWeight() { return weight; } public void setWeight(int weight) { this.weight = weight; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean equals(Object obj) { if (obj == this) return true; if (obj instanceof Birds) { if (!((Birds) obj).name.equals(name)) return false; if (!(((Birds)obj).weight == weight)) return false; } return true; } // public boolean equals(Object anObject) { // // TODO Auto-generated method stub // //如果是引用类型 判断内存地址是否相同 // //如果是 基本数据类型 判断值是否相等 // if (this == anObject) { // return true; // } // if (anObject instanceof String) { // //判断类里的属性值是否相等 // //相同 return true; // //不相同 return false; // } // return true; // } }
主函数:
class Main { public static void main(String[] agrs) { Birds b = new Birds(); Birds c = new Birds(); b.setName("abc"); c.setName("abc"); System.out.println(b.equals(b)); System.out.println(b.equals(c)); } }
结果是:
true true
hashCode()和equals()方法:
public class Birds {
private int weight;
private String name;
public Birds(){
this.weight = 140;
this.name = "huanghe";
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int hashCode(Object obj) {
int result = 1;
int prime = 31;
Birds b = (Birds) obj;
result = result * prime + weight;
result = result * prime + ((b.name.equals(name))?0:(b.name).hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
Birds b = (Birds) obj;
if (weight != b.weight) return false;
if (name != b.name) return false;
return true;
}
}
class Main { public static void main(String[] agrs) { Birds b = new Birds(); Birds c = new Birds(); b.setName("abc"); c.setName("abd"); System.out.println(b.equals(b)); System.out.println(b.equals(c)); } }
结果:
true
false
相关文章推荐
- Java中判断对象是否相等的equals()方法使用教程
- Java通用判断对象是否相等方法
- java中判断两个对象是否相等的方法和代码
- 判断两个对象是否相等,为什么必须同时重写equals()和hashcode()方法
- js中判断Object、Array、Function等引用类型对象是否相等的方法
- java 判断两个对象是否相等 equals 和 hashcode() 的必要性
- 判断java中两个对象是否相等
- 判断两个对象是否相等,为什么必须同时重写equals()和hashcode()方法
- [置顶] 判断两个对象是否相等,hashcode和equals方法的联系
- 原!! java直接打印一个对象时,并不是直接调用该类的toString方法 ,而是会先判断是否为null,非null才会调用toString方法
- Java 中判断两个对象是否相等
- JAVA--HashCode 基础(判断对象是否相等)
- java判断对象是否为数组 并且取得数组的值 --- 用instanceof或则isArray() 方法
- java中一切皆对象——判断字符串是否相等的方式
- 马士兵Java课堂笔记-6-容器Collection接口remove方法中判断是否相等并删除问题
- 判断对象是否相等---重写equals方法
- java integer对象判断两个数字是否相等
- 深入java的equals方法,怎么比较两个对象的内容是否相等?对象的内容指的是什么?
- java怎么判断两个Set 里的对象的值是否相同【两个set中的值是否相等】、java treeset和hashset如何判断元素是否相同【即对象是否完全相同;利用一个set去除重复元素】
- JAVA--HashCode 基础(判断对象是否相等)