0

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?

2
  • Show us some code. Looks like you're trying to use byte[] in place of a string. Commented Sep 30, 2012 at 2:53
  • 1
    Solved : Just type casted the HashMap it to a Map and retrieved the value as an object (typecasting the value again to byte[]).. Commented Sep 30, 2012 at 7:48

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, you are right, the method is doing @SuppressWarnings("unchecked").. I was not sure why this was there earlier. This method is involved in the process of which I am retrieving my data..So looks like someone before me purposefully did this.. so what should I do to retrieve byte data from such a map?
I think you should fix the code that is putting a byte[] value into the MappedRecord. It is wrong, wrong, wrong!!!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.