重写hashCode方法
来源:互联网 发布:网络渗透软件 编辑:程序博客网 时间:2024/05/01 05:42
//什么时候重修hashCode 方法? 当对象当作键值或者索引的时候 需要重写
import java.util.*;
class Name{
private String name;
private String age;
public Name(String name,String age){
this.name=name;
this.age=age;
}
public String toString(){
return this.name+" "+this.age;
}
public boolean equals(Object obj){
if(obj instanceof Name){
Name n=(Name)obj;
return (name.equals(n.name)) && (age.equals(n.age));
}else
return super.equals(obj);
}
public int hashCode(){
return name.hashCode();
}
}
public class Stu{
public static void main (String [] args)
{
Collection hash=new HashSet();
//添加元素
hash.add("hello");
hash.add(new Integer(200));
hash.add(new Name("majianjie","22"));
//删除元素
hash.remove("hello");
hash.remove(new Integer(200));
System.out.println(hash.remove(new Name("majianjie","22"))); //在没有重写hashcode的方法时候,删除不了的,返回 false!
System.out.println(hash);
}
}
0 0
- 重写一下hashCode方法
- 重写 hashCode 方法
- 重写hashCode方法
- 如何重写hashcode方法
- 重写hashCode方法
- 重写hashCode方法
- 重写hashCode()方法
- 重写hashCode方法
- hashcode的重写方法
- 重写hashCode()方法和重写equals()方法
- 重写自己的hashCode()方法
- 重写自己的hashCode()方法
- 重写自己的hashCode()方法
- 重写hashCode和equals方法
- 为什么要重写hashcode() 方法
- 重写Object中的hashCode方法
- 重写equals和hashCode方法
- 重写hashCode和equals方法
- 动态绑定(多态)
- 接口的简单应用
- 黑马程序员_JAVA学习笔记12
- SqlParameter[]写法
- 增强版for循环
- 重写hashCode方法
- ASP.NET MVC4+ORACLE+EF+EXTJS4.2.1开发
- android跨进程通信(IPC):使用AIDL
- 遍历磁盘
- 记得常回头看看以前犯的错误,但绝不要为它们感到后悔
- Android内核解读-Android系统的开机启动过程
- UVA 1456 - Cellular Network(dp + 贪心)
- android网站
- 程序员如何快速准备面试中的算法