0

I'm new to Java, but know Objective-C. I need to access fields < keys, values > in a downloaded Object.

Below is the code:

car is an Schema and car_id is the field to query

Map<String, List<SMObject>> feedback = new HashMap<String, List<SMObject>>();
List<SMCondition> query = new ArrayList<SMCondition>();
DataService ds = serviceProvider.getDataService();
List<SMObject> results;

try {
            query.add(new SMEquals("car_id", new SMString(make)));
            results = ds.readObjects("car", query);

            if (results != null && results.size() > 0) {
                feedback.put(make, results);
            }

    }
....

results is an Object downloaded from a remote Database that is basically a HashMap. Assuming there is only one object that is returned each time, what would be the code to access Key & Values in the returned results object?

Complete Code in case you want to see it.

EDIT

Can I do something like this:

    SMObject resultObj;

     if (results != null && results.size() > 0) {
            resultObj = results[0];
            resultObj.put("resolved", "1");
            resultObj.put("accepted", "1");
            resultObj.put("declined", "0");

            String model = (String)resultObj.get("model");
        }
user1107173
  • 10,334
  • 16
  • 72
  • 117
  • 1
    That completely depends on what your `SMObject` class is. – SLaks Sep 17 '13 at 23:45
  • If this question is about iterating the map, see http://stackoverflow.com/q/46898/260633. That being said it is unclear what object you are referring to in the question. The `Map` or the `SMObject`. – Dev Sep 17 '13 at 23:47
  • Thanks. I'm sorry. results. – user1107173 Sep 17 '13 at 23:52

1 Answers1

1

If you wanted all the keys, you would do:

Map<String, List<SMObject>> feedback = new HashMap<String, List<SMObject>>();
List<String> myKeys = feedback.keySet();

To get the values, you would use the get method:

Map<String, List<SMObject>> feedback = new HashMap<String, List<SMObject>>();
feedback.get("yourKey"); 

For more info, check out: http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

EDIT:

SMObject resultObj;

 if (results != null && results.size() > 0) {
        List<SMObject> myResults = feedback.get(make); 
        resultObj = myResults.get(0);
        resultObj.put("resolved", "1");
        resultObj.put("accepted", "1");
        resultObj.put("declined", "0");

        String model = (String)resultObj.get("model");
    }

The general concept is that you use the key, to get the value from the hashMap. That value happens to be a list of objects; therefore, you need to iterate over that list as well and retrieve each object from the list.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
  • Thanks! Just to clarify, I was under the impression that results is a type of Array of objects. 'make' is the key that calls that array. Will your code allow me to access the results[0] and it's ? – user1107173 Sep 17 '13 at 23:51
  • If I understand you correctly, yes. You could use `mResults = feedback.get(make);` would return the array of objects, at which point you would use `mResults[0]` to get that first result. – BlackHatSamurai Sep 18 '13 at 00:09
  • I'm lost on what you are trying to do. Are you trying to add keys/values to the list, or retrieve them? – BlackHatSamurai Sep 18 '13 at 00:15
  • If myResults is a `List`, you'd have to use `myResults.get(0)` to get by index. `myResults[0]` will only work for an array. Other than that, Blaine's approach looks good. And it is conceivable that `SMObject` wraps an internal HashMap but offers a different external API -- if `put(key)` is not available, look for `putString(key)`, `setAttribute(key)`, `set(key)` or other similar keyed put/set/add methods. – Thomas W Sep 18 '13 at 00:46
  • @ThomasW, you're right. In my haste, I forgot about that. I've updated. Thanks. – BlackHatSamurai Sep 18 '13 at 17:22