简体   繁体   English

HashMap在android中保持稳定

[英]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. 嗨我想在HashMap中保存一个对象,如果它不存在,那么如果它是exsit我想用新数据控制它的值。 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. 您的问题是:数组上的equals()不会对数组内容进行比较。 In other words: this call to equals() only gives "true" if the arrays you are comparing ... are one and the same , like in: 换句话说:如果你要比较的数组是一个且相同的话,这个对equals()的调用只给出“true”,如:

  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. 在比较数组内容时,您应该使用ArrayList。 Because two ArrayList objects are equal when they contain exactly the same equal elements. 因为两个ArrayList对象在包含完全相同的相等元素时是相等的

And you see: you are using that equals on arrays to make a decision in your code. 你会看到:你在数组上使用等于你的代码做出决定。 So, you either change to ArrayLists; 所以,你要么改为ArrayLists; or use Arrays.equals() as user hamsty suggested. 或者使用Arrays.equals()作为用户hamsty建议。

Just a few additions to the already posted answers. 只是添加了已经发布的答案。

The remove below is not necessary, a simple put will replace the old value 删除下面没有必要,简单的put将替换旧值

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

From the javadoc 来自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.) (当且仅当m.containsKey(k)返回true时,地图m才包含密钥k的映射。)

Have you considered making bytes 15-19 into a string and adding them onto the maps key? 您是否考虑将字节15-19转换为字符串并将其添加到地图键中? 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]; macId不在mMacMap ,插入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 macId中的mMacMap ,由于其他答案提到的数组比较问题,上一步创建的数组永远不会匹配integer0 ,使用对macId的引用替换地图中的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. macId中的mMacMap ,因为该数组是对integer0的引用,它将始终进行正比较,并且内容将不再更新。
  4. Repeat 3. 重复3。

Basically caused by these two issues: 基本上由这两个问题引起:

  1. Array#equals does not behave intuitively, use the static method Arrays.equals Array#equals行为不直观,使用静态方法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; Java是基于参考的,所以如果你在地图中插入一些东西,它将不会被复制,而只是创建一个新的引用; this may bite you if you change a shared object afterwards (like the array). 如果您之后更改共享对象(如数组),这可能会咬你。

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

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