ContentValues() incorrectly adding values
I'm using the following code to fill up a ContentValues variable.
public ContentValues getContentValues() {
ContentValues initialValues = new ContentValues();
initialValues.put("blt_name", m_name);
initialValues.put("blt_pictureURI", m_pictureURI);
initialValues.put("blt_description", m_description);
initialValues.put("b开发者_Python百科lt_UUID", getUUID().toString());
return initialValues;
}
My problem is that put() is putting the UUID and the name in the same hash location! I have no idea why. When creating the initialValues variable it creates an internal hashmap having 7 slots. When putting the values, key is added in slot 0, name is ALSO added in slot 0 (overwriting uuid), pic is added in slot 3 and desc is added in slot 7.
All four keys are, of course, different values, declared as final Strings.
I tried new ContentValues(4) in order to force them into the right spot, that was worse. 2 values were overwritten.
[Edit] I just tried changing the order of the puts. By moving the UUID so that it is put() last, it still overwrites slot 0 in hashmap. (I know what you are thinking, and YES the keys are unique.)
[EDIT] I tried it with the following code, and it works perfectly. I'm at a lost. I also edited the original question because I tried it with hard coded strings, and that didn't work either.
initialValues.put("a", m_name);
initialValues.put("b", m_pictureURI);
initialValues.put("c", m_description);
initialValues.put("d", getUUID().toString());
Any help would be appreciated,
-I_Artist
Are you sure that this is a problem? ContentValues is essentially a hash table, not an array. It's almost inevitable that there will be collisions between different keys. But collisions do not mean that you've lost your data. The only real way to be sure that your data is (or is not) stored appropriately is to try to get the data from the ContentValues object:
String newName = initialValues.get("blt_name");
String newPicture = initialValues.get("blt_pictureURI");
String newDesc = initialValues.get("blt_description");
String newUUID = initialValues.get("blt_UUID");
// now do something with these values to check if they're right...
I bet you'll find that the data have their correct values. If not, there's more going on than the code that you've posted can show us.
What happens if you hard code your keys as "Key1", "Key2", "Key3", "Key4"? I know you said that you are sure your keys are unique, however I am still curious if there could be something we are all not seeing.. Maybe you could show us an example of the values being set for the keys and their values?
(Ha! First time using stackoverflow, obviously this is not an answer...)
(So this might actually be your answer)
The HashMap computes an index into that array using the hashCode() of the key. It doesn't just use the hashCode() modulo the array size, but rather uses a more complex function of the hashCode().
It may be possible that the keys: "blt_UUID" AND "blt_name" are being hashed to the same value. This being the case, the two keys are being given the same index and a "collision" is occurring and the value is being overwritten. Try changing the key to something else, maybe using all capitalization, and try it again.
Best of luck.
精彩评论