0

Could someone explain why this works:

Map[] IEXDivMap = new Map[IEXJsonArray.length()];

    for (int i = 0; i < IEXJsonArray.length(); i++) {
        IEXDivMap[i] = new HashMap();
        JSONObject IEXJsonObject = IEXJsonArray.getJSONObject(i);

        IEXDivMap[i].put("exDate",IEXJsonObject.getString("exDate"));
        IEXDivMap[i].put("amount",IEXJsonObject.getString("amount"));            

    }

but this doesn't:

Object[] IEXDivMap = new Object[IEXJsonArray.length()];

    for (int i = 0; i < IEXJsonArray.length(); i++) {
        IEXDivMap[i] = new HashMap();
        JSONObject IEXJsonObject = IEXJsonArray.getJSONObject(i);

        IEXDivMap[i].put("exDate",IEXJsonObject.getString("exDate"));
        IEXDivMap[i].put("amount",IEXJsonObject.getString("amount"));            

    } 

Why can't I have an array of Objects each object being a hashmap?

DCR
  • 14,737
  • 12
  • 52
  • 115
  • You can but you need to cast the `IEXDivMap[i].put` to `((Map) IEXDivMap[i]).put`, the reason being that java doesn't know of the exact type of object you're adding into your `Object[]`. – Thanos Nov 23 '18 at 16:03

2 Answers2

3

You have to cast the Object to a Map.

Object[] IEXDivMap = new Object[IEXJsonArray.length()];

for (int i = 0; i < IEXJsonArray.length(); i++) {
    IEXDivMap[i] = new HashMap();
    JSONObject IEXJsonObject = IEXJsonArray.getJSONObject(i);

    IEXDivMap[i].put("exDate",IEXJsonObject.getString("exDate")); // this fails
    IEXDivMap[i].put("amount",IEXJsonObject.getString("amount"));

    ((Map) IEXDivMap[i]).put("exDate",IEXJsonObject.getString("exDate")); // this works
    ((HashMap) IEXDivMap[i]).put("exDate",IEXJsonObject.getString("exDate")); // this works           

} 

Object doesn't have a put method.

See also this question about casting.

Master_ex
  • 789
  • 6
  • 12
  • Java seems really ugly compared to javascript in how to access dictionaries. Is there anyway to use someObject[i]["someKey"] to access the content? I guess these would be Map literals which Java doesn't have yet – DCR Nov 23 '18 at 16:24
  • Not really, you have to use the `get()` method. However if you use the someObject[i] often in your block maybe it would be more readable to store it in a variable i.e. `Map map = someObject[i];` and then use it like `map.get("someKey");`. – Master_ex Nov 23 '18 at 16:28
1

You can definitely have an array of Objects where each object is a HashMap. The only problem with your second code snippet is that at runtime, the compiler doesn't know the type of IEXDivMap[i] (it doesn't know that it points to an object of type HashMap). So at that point of time, it will only expose to the user those methods which are defined on the Object class itself and not those defined in the HashMap class.

If you cast the IEXDivMap[i] to a HashMap like -> ((HashMap)IEXDivMap[i]) then the compiler invokes the methods defined in the HashMap class if the object referenced by IEXDivMap[i] is actually a HashMap

mettleap
  • 1,390
  • 8
  • 17