0

I have something like:

Map<String, Object> hashMap;

When I do something like:

hashMap.get("binary"), I get value as: {size=5642, name=abc}

Here key is binary and value is an Object of Type Object and is {size=5642, name=abc}

Note the values dont belong to a particular class. In Python I can do something like hashMap["binary"]["size"], was wondering what would be the equivalent in java

How do I get the value of size directly without parsing the above string?

NoobProg
  • 81
  • 1
  • 4
  • 10
  • How would you get the field of the object if it was held in a separate variable? – kabanus Jun 16 '20 at 05:48
  • 1
    Does this answer your question? [Convert string representing key-value pairs to Map](https://stackoverflow.com/questions/14768171/convert-string-representing-key-value-pairs-to-map) – ZhekaKozlov Jun 16 '20 at 05:49
  • 2
    That totally depends on what you put **into** the hashMap, but you're not showing us that. Using `Object` as the value type is very wide. If you want some specific field of the value, you need to declare the value type as something more specific like `Map` – Erwin Bolwidt Jun 16 '20 at 05:49
  • I edited the question. Is it clearer now? – NoobProg Jun 16 '20 at 05:57
  • 5
    You have to cast the retrieved Object to its actual class and then you can call getSize() if it has a getter with that name on it. – muasif80 Jun 16 '20 at 05:59
  • Does this answer your question? [How to get the fields in an Object via reflection?](https://stackoverflow.com/questions/2989560/how-to-get-the-fields-in-an-object-via-reflection) – Deepak Patankar Jun 16 '20 at 06:01

1 Answers1

2

The value is not of Type Object, but of some type that extends from Object (in java everything extends Object implicitly). Let's call it "X"

Now, it doesn't work like python because unlike python java doesn't have that dynamic nature.

{size=5642, name=abc} is probably a string representation of that type X. This is what you see in a debugger or maybe when trying to print the value on console with System.out.println or something.

Now first of all figure out which type is it:

Object value = hashMap.get("binary")
System.out.println(value.getClass().getName());

It will print the class name

Then check the source of that class, probably it looks like this:

public class X {
   private final int size;
   private final String name;

   ... // constructor, other stuff maybe


   // these are called "getters" in java world
   public int getSize() {return size;}
   public String getName() {return name;}
}

From that point you have 2 ways to get the size:

Object value = hashMap.get("binary");
int size = ((X)value).getSize();  // This is called downcasting

The drawback of this method is that you don't utilize the power of generics

So the better option is a refactoring if its possible of course:

Map<String, X> hashMap = ...
X value = hashMap.get("binary");
value.getSize();

One final note:

If it happens that the value is of type String, you won't be able to get the size other than parsing the value with regular expression or something. In this case consider a refactoring as a better option.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • Thanks for the detailed explanation! Really helpful.I found that the class is java.util.LinkedHashMap[Result of value.getClass().getName()] – NoobProg Jun 16 '20 at 06:44