B B
B B

Reputation: 149

byte array in HashMap of Strings

I am retrieving a map of user date, which dataMap of type HashMap<String, String>, and it has this key value pair, String, and byte[] of size 6.

Has anybody seen this before or know what to do?

Upvotes: 0

Views: 2057

Answers (2)

Stephen C
Stephen C

Reputation: 718708

There is something very fishy about your code.

These statements imply that MappedRecord must implement Map<String, String>.

record = (MappedRecord) obj;
item = new HashMap<String, String>();
item.putAll(record);

But then you say that this is inserting an entry whose value type is byte[]. This is possible, but it must mean that somewhere / somehow you have previously added that entry to your MappedRecord object. And in order for that to happen, you must be either suppressing or ignoring "unchecked conversion" warnings.

(Note that the putAll code doesn't check that the entries that it is adding to item have the right key and value types. It can't! The code for HashMap.putAll doesn't know what the parameter types should be ... due to type erasure. Rather, the putAll code assumes that the types of the actual keys and values are correct. And they should be ... unless you have ignored / suppressed the warnings.)

Either way, we won't be able to properly diagnose this without seeing the code of the MappedRecord class, and the code that is creating the MappedRecord instance that has the bogus entry in it.

Upvotes: 2

Hot Licks
Hot Licks

Reputation: 47699

Presumably this is Java.

While the declaration HashMap<String, String> states that the HashMap is expected to be String->String, the static type checking in the compiler is not airtight (given that it's kind of a kluge tacked onto the previously-existing language).

And there is no dynamic type checking to assure that (A) the HashMap you have really is a HashMap<String, String>, and (B) someone hasn't inserted an array into a HashMap<String, String>. This is because in reality what you have is a HashMap<Object, Object>, so there's no way to implement either dynamic check.

Upvotes: 0

Related Questions