简体   繁体   English

无法使用HttpSession作为密钥从HashMap检索值

[英]Fail to retrieve value from HashMap with HttpSession as key

I have a HashMap where I am putting HttpSession object as Key and HashSet as Value 我有一个HashMap,我将HttpSession对象作为Key,将HashSet作为Value

HashMap<HttpSession, HashSet<String>> map = new HashMap<HttpSession, HashSet<String>>();

I have a HttpSession listener where I need to check weather session object is exist in the Map 我有一个HttpSession监听器,我需要检查地图中是否存在天气会话对象

public void sessionDestroyed(HttpSessionEvent se) {
    HashMap<HttpSession, HashSet<String>> map = //Get map object
    HttpSession session = se.getSession();
    if(map.containsKey(session)){
        //TODO Code goes here
    }
}

IF condition always return false, even though session object is available in the map (manually checked the map entries). IF条件总是返回false,即使会话对象在映射中可用(手动检查映射条目)。 I am stuck!!! 我被困了!!!

If the value was put in the map in a previous request, the HttpSession object that you have now is not necessarily the same that is present in the map (even though they represent the same session). 如果在先前的请求中将值放入映射中,则您现在拥有的HttpSession对象不一定与映射中存在的对象相同(即使它们代表相同的会话)。 Two representations of the same session are not required (AFAIK) to be equals or to have the same hashCode , which will break the map. 不需要同一会话的两个表示(AFAIK) equals或具有相同的hashCode ,这将破坏映射。

You may try using the session.getId() value as key. 您可以尝试使用session.getId()值作为键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM