I would like to get the value of a byte inside a Map object.
Map<String, Object> data = new HashMap<>();
data.put("foo", new byte[]{4});
System.out.println(data.get("foo"));
What I'm getting is like this... [B@1a779dce
Please help me.
I would like to get the value of a byte inside a Map object.
Map<String, Object> data = new HashMap<>();
data.put("foo", new byte[]{4});
System.out.println(data.get("foo"));
What I'm getting is like this... [B@1a779dce
Please help me.
As @JigarJoshi mentioned in his comment, that is what the toString()
implementation for byte[]
class look like. When you do System.out.println
on a byte[]
, the toString()
method gets called to convert it to a String
before printing.
If you want to read the individual bytes, you will have to loop through the return value.
byte[] ba = (byte[]) data.get("foo");
for (byte b: ba) {
System.out.println(b);
}
As you are adding Object
to Map not byte[]
so you need to cast it.
Map<String, Object> data = new HashMap<>();
data.put("foo", new byte[]{4,3,1,2});
System.out.println(Arrays.toString((byte[])data.get("foo")));
OUTPUT
[4,3,1,2]