java: Store HashMap<String, String> in ArrayList<Object> - java

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

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

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.

use array list of hashmap
ArrayList<HashMap<String, String>>()

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

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.

Related

How to Add Map<String, Object> elements in firebase firestore android using Hashmap? A Store Database

With the last improvement in matrices, you can add, update and delete an element but only in matrix.
But for:
Map<String, Object> comentarios = new HashMap<>();
I probe it and it transforms me a Map to array does not matiene the same type.
Someone know how I should do it?
My code is:
final Map<String, Object> nuevoComentario = new HashMap<>();
nuevoComentario.put("comentario", FieldValue.arrayUnion("id5", "Msj add"));
db.collection("Hospedaje").document(documentGetID)
.update(nuevoComentario);
From what I understand FieldValue.arrayUnion and FieldValue.arrayRemove are just for array fields, which HashMap isn't really.
Try using db.collection("Hospedaje").document(documentGetID).update("myHashMapField", "key" to /value/);

Java Array Name from the request parameter

I have an array
String[] strArray= {"Milk","Bread,"eggs"};
and generally we can display the values as strArray[i] in a loop ... however I am getting the name of the array " StrArray" dynamically from a value of a request parameter &arrayName=strArray .
request.getParameter("arrayName");
Can one of you experts kindly advise how to construct the arrayname from the request and use it to display the values of the array.
The easiest but unclean way would be a map:
Map<String, String[]> map = new HashMap<String, String[]>();
map.put("strArray", strArray);
map.get("strArray");
Probably better would be to use reflection.
stackoverflow example
HashMap<String, String[]> strMap = new HashMap<String, String[]>();
strMap.put("strArray", new String[]{"Milk","Bread,"eggs"});
String strName[]=(String[])strMap.get(request.getParameter("strArray"));
response.write(strName[1]);
this works.

Storing multiple datatypes in a single HashMap

I want to put an array of int and a String into a HashMap. Is this possible? what is the proper way to that?
This is my HashMap:
Map<String, String> stringMap = new HashMap<>();
stringMap.put("Text","fish" );
stringMap.put("Diet","false");
stringMap.put("CookingTime", "60");
stringMap.put("OptionId", [7,8,8]);
I know this line is wrong - how do I store an array in a HashMap?
stringMap.put("OptionId", [7,8,8]);
You can instantiate an array in java by doing
someMap.put("OptionId", new int[]{7,8,8});
Otherwise you will need a function that returns those values in an array.
For your case: if you want to create a HashMap of multiple datatypes you can use
HashMap<String, Object> map = new HashMap<String, Object>();
You can then put anything you want in
map.put("key1", "A String");
map.put("key2", new int[]{7,8,8});
map.put("key3", 123);
Except now the tricky part is you don't know what is what so you need to use instanceof to parse the map unless you know what type of object is at a key then you can just cast it to that type.
if(map.get("key1") instanceof String)
String s = (String) map.get("key1"); // s = "A String"
or
int[] arr = (int[]) map.get("key2"); // arr = {7,8,8}

search string in HashMap combine ArrayList on android

I am trying listItem.indexOf(word.text)==-1 to find the string I want.
Using indexOf function in single ArrayList is working fine. but after I use HashMap combine ArrayList, the indexOf function seems not working.
Any solution? thx!
ArrayList<HashMap<String, Object>> listItem= new ArrayList<HashMap<String,Object>>();
SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem, R.layout.item_main, new String[] {"ItemImage","ItemTitle", "ItemText"},
new int[] {R.id.ItemImage,R.id.ItemTitle,R.id.ItemText});
if(listItem.indexOf(word.text)==-1){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", R.drawable.speak2);
map.put("ItemTitle", word.text);
map.put("ItemText", temp);
listItem.add(map);
}
I'm assuming that word.text is a String. Therefore you have no reason to expect that listItem.indexOf(word.text) would find it in a List that contains HashMap instances.
It looks like you are storing word.text as one of the values of a HashMap stored in the list. To search for a HashMap in the List that contains this value you have to iterate over all the Maps in the List, and for each Map, iterate over all its values.
Perhaps a better data structure would be HashMap<String,HashMap<String, Object>, where the key is taken from word.text and the value is the HashMap you currently store in the list. This way, you could replace listItem.indexOf(word.text) with mapItem.containsKey(word.text), which is more efficient, and would work.
HashMap<String,HashMap<String, Object>> mapItem= new HashMap<String,HashMap<String,Object>>();
if(!mapItem.containsKey(word.text)){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("ItemImage", R.drawable.speak2);
map.put("ItemTitle", word.text);
map.put("ItemText", temp);
mapItem.put(word.text, map);
}

How can I put a hashmap inside an array? conf[0] = new HashMap<String, Object>();

How can I put a hashmap inside an array?
HashMap<String, Object>[] config= null;
config[0] = new HashMap<String, Object>();
config[0].put("Name", "Jon");
config[0].put("valueA", 0);
config[1] = new HashMap<String, Object>();
config[1].put("valueA", 2323);
List<Map<String, String>[]> listOfMaps = new ArrayList<Map<String, String>[]>();
should be sufficient. You don't instantiate it with the () since it is an array, you need to provide it a size or a series of HashMaps as part of the array constructor.
You can find some explanation here What's the reason I can't create generic array types in Java?
I propose to use next construction:
ArrayList<HashMap<String, Object>> config= new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> map;
map = new HashMap<String, Object>();
map.put("Name", "Jon");
map.put("valueA", 0);
config.add(map);
map = new HashMap<String, Object>();
map.put("valueA", 2323);
config.add(map);
Whilst you can declare an array of generics, you cannot instantiate them using the normal syntax.
Map<String,Object>[] config = new HashMap<String,Object>[10]; would give the compile error:
Cannot create a generic array of HashMap
You can however use Array.newInstance see answers to this SO question
use ArrayList instead of a simple array.
ArrayList<HashMap<String, Object>> arrayOfMap=new ArrayList<HashMap<String,Object>>();
Map<String,Object>[] config = new HashMap[10];
just leave the generic parameters away.

Categories