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.
Related
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/);
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}
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.
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);
}
I'd like to explore the option of using a HashMap to keep track of changes between files. I'm using a few config/text files to give a set of documents of status:
The config file looks like:
STATUS1 = "Doc1.pdf, Doc2.xls, Doc5.doc"
STATUS2 = "Doc8.pdf, Doc6.doc"
STATUS3 = "Doc10.pdf"
...
Instead of having to create a separate HashMap for each instance like so:
Map<String, String> map1 = new HashMap<String, String>();
Map<String, String> map2 = new HashMap<String, String>();
Map<String, String> map3 = new HashMap<String, String>();
map1.put("STATUS1", "Doc1.pdf");
map2.put("STATUS1", "Doc2.xls");
map3.put("STATUS1", "Doc5.doc");
I'd like to have only a single Map with the key of the status and the values mapped to that key.
I don't need help in parsing the file, I just need assistance in implementing the HashMap or Map so I can add this functionality. If there are other datatypes or methods of organizing this data, I'd like to hear your opinions on that.
Any help would be much appreciated.
You can use a MultiMap, which stores multiple values for the same key.
Multimap
Multimap<String, String> myMultimap = ArrayListMultimap.create();
// Adding some key/value
myMultimap.put("STATUS1", "somePDF");
myMultimap.put("STATUS1", "someDOC");
myMultimap.put("STATUS1", "someXCL");
myMultimap.put("STATUS2","someFormat");
// Getting the size
int size = myMultimap.size();
System.out.println(size); // 4
// Getting values
Collection<string> stats1 = myMultimap.get("STATUS1");
System.out.println(stats1); // [somePDF, someDOC, someXCL]
2 . HashMap
With HashMap you can have something like,
List<String> listOfDocs = new ArrayList<String>();
listOfDocs.add("somePDF");
listOfDocs.add("someDOC");
listOfDocs.add("someFormat");
HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
// key would be your STATUS
// Values would be ListOfDocs you need.
map.put("STATUS1", listOfDocs);
map.put("STATUS2", listOfDocs2);
map.put("STATUS3", listOfDocs3);
Hope this helps.
Let me know if you have questions.