简体   繁体   中英

Java Hashmap Iterator Issue on conditon

I am trying to loop through a hashmap which contains Sessions and IDs. Multiple sessions may have the same ID. On each method call, I need to iterate through the hashmap and find which sessions are listed against a given ID.

class contains:

private static Map<Session, String> peers = new HashMap<Session, String>    ();

Method contains:

for (Map.Entry<Session, String> entry : peers.entrySet()) {
                if(entry.getValue() == clientId){
                    Session peer = entry.getKey();
                    peer.getBasicRemote().sendObject(figure);
                   }
            }

But the problem is it runs only one time. Even I tried to get the size of hashmap and it given the exactly amount what I have.

As the value is a String, you should probably compare it via equals(), as == only checks if two objects are the same object, but not if they are the same String. But that's only guessing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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