I add few key-value pairs to a hashmap.
After I add key-value pair, when I print the size of hashmap, I get the size as 1. When I print the value at another place(after adding value to key-hashmap), I get the size of hashmap as zero. I do not remove the value added to this hashmap from this class or any other external classes. Then , how the hashmap size becomes zero? Can someone please explain?
Any help is appreciated.
Code here:
private HashMap <Context,BLEEventListeners> mHashMapCallbacks = new HashMap<Context,BLEEventListeners>();
public void startTimeServer(BLEEventListeners eventListener,Context context) {
mHashMapCallbacks.put(context, eventListener);
Log.d(TAG,"****Inside startTimeServer,mHashMapCallbacks size: " +mHashMapCallbacks.size());// I get 1 as size
Intent cmn_intent = new Intent(IServerCommon.class.getName());
Intent time_intent = new Intent(ITimeServer.class.getName());
mContext.bindService(time_intent, time_connection, Context.BIND_AUTO_CREATE);
mContext.bindService(cmn_intent, cmn_connection, Context.BIND_AUTO_CREATE);
}
private ICommonResultCallback callback = new ICommonResultCallback.Stub() {
public void receiveMessage(Bundle value) throws RemoteException {
Log.d(TAG,"****Inside connected,mHashMapCallbacks size: " +mHashMapCallbacks.size());// I get 0 as size
}
}
ICommonResultCallback() looks like a callback function. If it is, then hashmap will be initialized again when this function is called. This is because a new Instance of you class is created for call back also.
You can verify this by using making hashmap “static”. It should then retain the value.