简体   繁体   中英

HashMap dont stay stable in android

Hi I am trying to save a object in HashMap if it is not exist than if it is exsit I want to control its value with new data. If data change than I want to do sth. else. But whenever I tried to compare new data and hash value I saw they are same on every time . How can I handle with this issue. There is code:

BluetoothLeDevice deviceLe;
private Map<String, byte[]> mMacMap;
byte [] integer0 =new byte[4];
byte[] tempInteger0=new byte[4];

public void addSensor(String macId, BluetoothLeDevice deviceLe) {
        byte [] addSensorrecord=deviceLe.getScanRecord();
        int j=0;
        for(int i=15;i<19;i++)
        {
            integer0 [j]=addSensorrecord[i];
            j++;
        }
        if (mMacMap.containsKey(macId)) {
            tempInteger0 = mMacMap.get(macId);

            if(!integer0 .equals(tempInteger0))
            {
                mMacMap.remove(macId);
                mMacMap.put(macId, integer0 );
                new SendBLEData().execute(deviceLe);
            }

        } else {
            final byte [] LocalInteger0=new byte[4];
            int t=0;
            for(int i=15;i<19;i++)
            {
                LocalInteger0[t]=addSensorrecord[i];
                t++;
            }
            mMacMap.put(macId, LocalInteger0);
            new SendBLEData().execute(deviceLe);
        }
    }

I am guessing, that your problem is here:

!Integer0.equals(tempInteger0))

I think you want to compare two arrays; and you are surprised to find them to be different ... all the time.

Your problem is: equals() on arrays doesn't do a comparison of the array content. In other words: this call to equals() only gives "true" if the arrays you are comparing ... are one and the same , like in:

  int a[] = { 1 };
  int b[] = a;
  int c[] = { 1 };

Here:

 a.equals(b) --> true

but

 a.equals(c) --> false

When comparing array content matters, then you should use ArrayList instead. Because two ArrayList objects are equal when they contain exactly the same equal elements.

And you see: you are using that equals on arrays to make a decision in your code. So, you either change to ArrayLists; or use Arrays.equals() as user hamsty suggested.

Just a few additions to the already posted answers.

The remove below is not necessary, a simple put will replace the old value

mMacMap.remove(macId);
mMacMap.put(macId, integer0 );

From the javadoc

If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.)

Have you considered making bytes 15-19 into a string and adding them onto the maps key? This would eliminate the array compare and make the lookups much faster.

 !Integer0.equals(tempInteger0)) 

is your problem.

Use this to compare the content of arrays:

Arrays.equals(Integer0, tempInteger0)

The problem is the following sequence of events:

  1. macId not in mMacMap , insert new byte[4]; into the map
  2. macId in mMacMap , the array created in the previous step never matches integer0 due to the Array comparison problem mentioned by the other answer, replace macId in the map with a reference to integer0
  3. macId in mMacMap , since the array is a reference to integer0 , it will always compare positively and the contents will no longer be updated.
  4. Repeat 3.

Basically caused by these two issues:

  1. Array#equals does not behave intuitively, use the static method Arrays.equals
  2. Java is heavily reference-based, so if you insert something into a map it will not be copied, but simply a new reference is created; this may bite you if you change a shared object afterwards (like the array).

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