问题描述
这是我的一段代码:public UserConnection getUserConnectionByToken(Token token) { // TODO Auto-generated method stub for (Iterator<Token> iter = (Iterator<Token>) TransactionManagerImpl.userConnections .keySet().iterator(); iter.hasNext();) { Token token1 = iter.next(); if(token1.equals(token)){ return userConnections.get(token1); } } return (UserConnection)userConnections.get(token); }其中userConnections是一个ConcurrentHashMap,里面的key是Token对象,value是UserConnection 对象,这是webservice服务端的一个方法,token是在客户端传过来的,都实现了序列化,但是在服务端处理的时候,直接 userConnections.get(token)却取不出来,但是通过迭代判断if(token1.equals(token)){return userConnections.get(token1);}却能取出来,这是怎么回事儿呢,哪位帮解释一下? 问题补充:beneo 写道
解决方案
你看看ConcurrentHashmap的get方法 /* Specialized implementations of map methods */ V get(Object key, int hash) { if (count != 0) { // read-volatile HashEntry<K,V> e = getFirst(hash); while (e != null) { if ([color=red]e.hash == hash && key.equals(e.key[/color])) { V v = e.value; if (v != null) return v; return readValueUnderLock(e); // recheck } e = e.next; } } return null; }看到红色部分么?你需要equals && == 你最好在hashcode相等的时候端点进去看看,是不是equals相等
解决方案二:
引用客户端打印的hashCode和服务端打印的hashCode值一样,但还是取不出来token1 和 token 的 hashCode 一样吗? 如果一样,肯定可以拿出来。
解决方案三:
用默认的hashcode,一般来说没有多少问题,除非你有特殊的要求非要使用自定义hashcode
解决方案四:
引用服务端的一个方法,token是在客户端传过来的,都实现了序列化这要在序列化之前就重写,序列化之后再重写就没意义了。
解决方案五:
你看看value是什么指,跟踪进去看看他hashcode的方法
解决方案六:
最简单打印所有的hashcode
解决方案七:
我说错了,反序列化和hashcode无关,如果你重写了hashcode,可能有隐患你的token不要重写hashcode,因为你如果都是一个object,但是因为string属性改变而使得hashcode改变,你的map就得不到,但是他们还是同样的object
解决方案八:
引用已经重写了equals和hashCode,Token只有一个String的属性看看你是如何重写的?
解决方案九:
map只是比较hashcode但是equals 是 ==所以出不来反序列化不能保证hashcode一致,你必须得重写hashcode