0
ArrayList<Object> parameters = new ArrayList<Object>();

HashMap<String, String> parameter = new HashMap<String, String>();
parameter.put("key", "value");

parameters.add(parameter);
parameters.add((String) "additionalData"); //this line is here for a reason
destinationFunction(parameters);

....

destinationFunction(ArrayList<Object> data){
     HashMap<String, String> imported = (HashMap<String, String>) data.get(0);
     String value = imported.get("key");
}

How do i achieve this? When i try i receive no errors up until like 2 of destinationFunction where i receive null pointer exception

7
  • try new ArrayList<HashMap<String, String>>(); definition Commented May 1, 2015 at 8:59
  • you can create arraylist of hashmaps Commented May 1, 2015 at 9:00
  • please post complete logcat. Commented May 1, 2015 at 9:00
  • would that prevent parameters.add("additionalData"); as the ArrayList is now of a specific object? Commented May 1, 2015 at 9:00
  • 1
    alternative solution would be to use JSON Objects and arrays. Commented May 1, 2015 at 9:08

5 Answers 5

1
ArrayList<Object> parameters = new ArrayList<Object>(); 

relpace this line with what you want to store in array list like you want to store hash map then create arraylist of type hash map

 ArrayList<HashMap<String, String>> parameters = new ArrayList<HashMap<String, String>>();

hope this will help you

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

Comments

0

Alternative Solution would be using JsonObject and JsonArrays. they are perfect for such "scenarios" (working with String,Integer and etc with combination of List and Map).

they may also be useful for complex object(e.g. working with Images) but will be abit more difficult to implement. in such cases please refer to java_serialization

in your case use JSONArray instead of your ArrayList and JsonObject instead of HashMap.

1 Comment

This solution solves the exact problem described in the question, where as implementing just an ArrayList of HashMap<String, String> would omit the single string value also contained in the ArrayList.
0

use array list of hashmap

ArrayList<HashMap<String, String>>()

Comments

0

Try to put ArrayList<HashMap<String, String>> instead of ArrayList<Object>

create a HashMap and add into the ArrayList. For Example:-

ArrayList<HashMap<String, String>> list = new ArrayList<>();

HashMap<String, String> map = new HashMap<>();
map.put("KEY", "VALUE");

list.add(map); //Add the map into list

Comments

0

You did not specified what type are you storing in your arrayList. But you assume in your destinationFunction that this list contains only HashMap<String, String> elements. Besides terrible code style it is okay in java to write so. But you made a mistake when put in your arrayList an element of type String which is breaking your assumptions in destinationFunction.

P.S. Try to write more type safe code and avoid generic types with Object type parameter.

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.