日期:2014-05-18 浏览次数:20751 次
public class ContextHolder {
public static Map<String, ScriptSession> onlineUsers = new HashMap<String, ScriptSession>();
public static void removeMapItemByKey(Map map,Object key){
Iterator<Map.Entry<Object, Object>> it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry<Object, Object> entry=it.next();
Object k=entry.getKey();
if(key.equals(k)){
//System.out.println("delete this: "+k+" = "+key);
it.remove(); //OK
}
}
}
}
JsonObject jo = new JsonObject();
String onlineUser = "";
Map<String, ScriptSession> m = new IdentityHashMap<String, ScriptSession>();
Iterator it = ContextHolder.onlineUsers.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, ScriptSession> pairs = (Map.Entry<String, ScriptSession>)it.next();
m.put(pairs.getKey(),pairs.getValue());
}
it = m.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, ScriptSession> pairs = (Map.Entry<String, ScriptSession>)it.next();
onlineUser += "".equals(onlineUser)?"'"+pairs.getKey()+"'":",'"+pairs.getKey()+"'";
}
jo.addProperty("onlineUser", "["+onlineUser+"]");
json = jo.toString();