HASHMAP的KEY值问题

eimhee 2009-12-03 10:43:14
我想把HASKMAP的KEY放为一个对象,
OBJECT中有region, location, department属性,
如果两个region和location,department都相同, 它们是不是对应同一个VALUE呢?

Class Employee {
String region;
String location;
String department;
}

Employee employee1;
Employee employee2;
这两个对象不是同一个引用, 但它们里面的属性的值是相同的,是否能做到这两个对象在HASHMAP中的KEY也是相同的
...全文
292 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
frank3G 2009-12-04
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 musiclee 的回复:]
学习 学习 !!!!!!!!!!!!!!!!!
[/Quote]学习 学习
musiclee 2009-12-04
  • 打赏
  • 举报
回复
学习 学习 !!!!!!!!!!!!!!!!!
amdgaming 2009-12-04
  • 打赏
  • 举报
回复
hashcode的实现有些 问题,看下面这个应该 是没什么问题了这样产生的hashcode重复的可能性非常之小。。。


@Override
public int hashCode()
{
return 7 * region.hashCode() + 9 * location.hashCode() + 13 * department.hashCode();
}




[Quote=引用 11 楼 eimhee 的回复:]
看来用数组不行,

public static void main(String[] args) {
Map map = new HashMap();
String[] temp = new String[3];
temp[0] = "1";
temp[1] = "2";
temp[2] = "3";
map.put(temp, "1");

String[] temp2 = new String[3];
temp2[0] = "1";
temp2[1] = "2";
temp2[2] = "3";

map.put(temp2, "1");
System.out.println(map.size());
}

输出是2,
[/Quote]
huntor 2009-12-04
  • 打赏
  • 举报
回复
Map的key推荐使用一个不可变得值。
Map的javadoc里提到
注:将可变对象用作映射键时必须格外小心。当对象是映射中某个键时,如果以影响 equals 比较的方式更改了对象的值,则映射的行为将是不确定的。此项禁止的一种特殊情况是不允许某个映射将自身作为一个键包含。虽然允许某个映射将自身作为值包含,但请格外小心:在这样的映射上 equals 和 hashCode 方法的定义将不再是明确的。
amdgaming 2009-12-03
  • 打赏
  • 举报
回复
我那样写吧。。楼主。。。你也可以 看看别人的回复。。。。
eimhee 2009-12-03
  • 打赏
  • 举报
回复
看来用数组不行,

public static void main(String[] args) {
Map map = new HashMap();
String[] temp = new String[3];
temp[0] = "1";
temp[1] = "2";
temp[2] = "3";
map.put(temp, "1");

String[] temp2 = new String[3];
temp2[0] = "1";
temp2[1] = "2";
temp2[2] = "3";

map.put(temp2, "1");
System.out.println(map.size());
}

输出是2,
bayougeng 2009-12-03
  • 打赏
  • 举报
回复
amdgaming的做法是合理的。差不多就是这个意思了。
amdgaming 2009-12-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 eimhee 的回复:]
弄一个数组为什么不用复写HASHCODE和EQUAL方法了呢
[/Quote]
你弄 arraylist可以不重写 hashcode和 equals方法么?

还是我 写的那个代码 那样 写 比较好 ,个人觉得。。。
eimhee 2009-12-03
  • 打赏
  • 举报
回复
弄一个数组为什么不用复写HASHCODE和EQUAL方法了呢
amdgaming 2009-12-03
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 eimhee 的回复:]
我想问一下,如果KEY如果为arraylist,当中放入
String region;
  String location;
  String department;
这三个属性,

List array = new ArrayList()
array.add(region)
array.add(location)
array.add(department)

再放入map当中, 这样也可以吗?
[/Quote]

这样 似乎 不太好。 还不如 弄 个 数组弄 存入 三个 元素 。呵呵

String[] temp = new String[3]
temp[0] = ;
temp[1] = ;
temp[2] = ;
amdgaming 2009-12-03
  • 打赏
  • 举报
回复
看下我写的,hashcode写法你可以自己 再 看看 改怎么写 。。

import java.util.HashMap;
import java.util.Map;

public class TestRegex extends Object {

/**
* @param args
*/
public static void main(String[] args) {

Map map = new HashMap();
map.put(new Employee("a", "shanghai", "zhengfu"), null);
map.put(new Employee("a", "shanghai", "zhengfu"), null);
System.out.println(map.size());
}
}

class Employee {
String region;
String location;
String department;

public Employee(String _region, String _location, String _department) {

this.region = _region;
this.location = _location;
this.department = _department;

}

public boolean equals(Object obj) {
if (!(obj instanceof Employee)) {
return false;
}
Employee p = (Employee) obj;
return p.region.equalsIgnoreCase(region)
&& p.location.equalsIgnoreCase(location)
&& p.department.equalsIgnoreCase(department);

}

@Override
public int hashCode()
{
return region.hashCode() + location.hashCode() + department.hashCode();
}
}
eimhee 2009-12-03
  • 打赏
  • 举报
回复
我想问一下,如果KEY如果为arraylist,当中放入
String region;
String location;
String department;
这三个属性,

List array = new ArrayList()
array.add(region)
array.add(location)
array.add(department)

再放入map当中, 这样也可以吗?
frank3G 2009-12-03
  • 打赏
  • 举报
回复
[Quote=引用楼主 eimhee 的回复:]
我想把HASKMAP的KEY放为一个对象,
OBJECT中有region, location, department属性,
如果两个region和location,department都相同, 它们是不是对应同一个VALUE呢?
。。。。
[/Quote]
不太理解LZ 下面部分说的问题;
两个对象属性值都相同,虽然相同,但是他们也两个,至于对应是不是一个VALUE,看你往VALUE里面放什么东西了。
bayougeng 2009-12-03
  • 打赏
  • 举报
回复
如果你用hibernate的话,你可以参考一下hibernate插件自动生成的POJO的代码。
对于两个实体是否相等,它的判断就是,主键值是否相等。跟你这个意思是一样的。但是hashCode代码是和有讲究的,你可以参考一下。
  • 打赏
  • 举报
回复
你实现一下 hashCode 和 equals 方法就可以了。

可以使用 Eclipse 自动生成的那个 hashCode 和 equals
bayougeng 2009-12-03
  • 打赏
  • 举报
回复
重写hashCode和equals方法。
注意一个原则:equals成立的话,hashCode必须相等。
hashCode相等的话,equals未必成立。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧