使用一个类作为hashMap的key
2013-07-22 16:00
274 查看
当使用一个类作为hashMap的key时,需要重写该类的equals()和hashCod()的方法
类的定义代码:
public class Object1{
private Long id;
private String code;
private String name;
private Integer type;
public Node(){
}
public Node(String code,String type){
this.setCode(code);
this.setType(type);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type =type;
}
public Integer getCode() {
return code;
}
public void setCode(String code) {
this.code =code;
}
public Integer getName() {
return name;
}
public void setName(String name) {
this.name =name;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Object1))
return false;
Node other = (Object1) obj;
if (other.getId() != null && this.getId() != null && other.getId().longValue() == this.getId().longValue()) {
return true;
}
if (other.getType() != null && this.getType() != null && other.getType() != null
&& this.geType() != null
&& other.getCode() == this.getCode()
&& other.getCode() == this.getCode()) {
return true;
}
return false;
}
@Override
public int hashCode() {
if(this.getCode() != null && this.getType() != null){
String hashCode = String.valueOf(getType()) + "_" + String.valueOf(getCode());
return hashCode.hashCode();
}
if(id != null){
return id.intValue();
}
return 0;
}
}
小插曲:
question:Using an object as key for a hashmap in Java
google answer:
The key needs to implement
The key must not be changed in any way that changes it's
Ideally any object used as a key in a
Objects that could otherwise be GCed might be kept around when they are used as key and/or value.
类的定义代码:
public class Object1{
private Long id;
private String code;
private String name;
private Integer type;
public Node(){
}
public Node(String code,String type){
this.setCode(code);
this.setType(type);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type =type;
}
public Integer getCode() {
return code;
}
public void setCode(String code) {
this.code =code;
}
public Integer getName() {
return name;
}
public void setName(String name) {
this.name =name;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Object1))
return false;
Node other = (Object1) obj;
if (other.getId() != null && this.getId() != null && other.getId().longValue() == this.getId().longValue()) {
return true;
}
if (other.getType() != null && this.getType() != null && other.getType() != null
&& this.geType() != null
&& other.getCode() == this.getCode()
&& other.getCode() == this.getCode()) {
return true;
}
return false;
}
@Override
public int hashCode() {
if(this.getCode() != null && this.getType() != null){
String hashCode = String.valueOf(getType()) + "_" + String.valueOf(getCode());
return hashCode.hashCode();
}
if(id != null){
return id.intValue();
}
return 0;
}
}
小插曲:
question:Using an object as key for a hashmap in Java
google answer:
The key needs to implement
.equals()and
.hashCode()correctly
The key must not be changed in any way that changes it's
.hashCode()value while it's used as the key
Ideally any object used as a key in a
HashMapshould be immutable. This would automatically ensure that 2. is always held true.
Objects that could otherwise be GCed might be kept around when they are used as key and/or value.
相关文章推荐
- java中hashMap使用一个对象作为key时,对key进行唯一性表达重写equals()方法
- HashMap使用对象作为key
- 一个关于自定义类型作为HashMap的key的问题
- 谨慎使用String作为HashMap的Key
- 谨慎使用String作为HashMap的Key
- Java HashMap使用String,Long,Integer作为key的性能测试
- HashCode 和 Equals 的使用 - 使用自定义对象作为HashMap的Key例子
- 历年世界杯冠军队伍如下,使用map存储这些队伍,使用年份作为key,冠军队伍名称作为value。 完成下面的功能: 1) 输入一个年份,如果当年有世界杯比赛,则输出获奖队伍,如果没有则输出“当年没有举
- ibatis使用HashMap作为返回结果时DB2,ORACLE,MYSQL对KEY大小写不同
- jmeter 如何将上一个请求的结果作为下一个请求的参数——使用正则提取器
- Java用自定义的类作为HashMap的key值实例
- StringBuilder万万不可作为HashMap的key
- GROUP BY的另种使用方法:将结果集一列的内容作为一个单元格显示
- 使用SQL查询语句时,数组动态赋值SQL IN ()作为条件条件一个
- STM32之中断与事件---一个使用GPIO作为外部中断的示例
- hibernate-memcached--在Hibernate中使用Memcached作为一个二级分布式缓存
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 使用一个消息队列作为计数信号量
- MOSS 2007 入门(6) : 使用一个Code-behind的用户控件作为WebPart
- hashmap判断是否存在key时,使用get(key)==null判断还是containsKey?