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);
}
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 have a map of JSONObject and Integer. I want to sort it in an incremental order using its values, here is what I've done so far:
Map<JSONObject, Integer> unsortedMap = new HashMap<>();
unsortedMap.put(jsonObject2.getJSONObject(key),key1);
Are you sure you want to use an Object as a key in a hashmap? It's normal practice to use immutable values for keys. The slightest change in any of the elements of the JSONObject will change the hash value and you will be unable to get your info back out of the map.
What is the integer value of your map?
Only a suggestion but you may want to check out the Java API for collection and see if there is anything that better suits your needs. Java Collection API
In the past I have wanted a sorted list with no duplicates. I had to first write to a TreeMap and then write to a SortedSet to get the result I wanted. Not code efficient, but I couldn't achieve what I wanted any other way.
Not sure if this is the best option but could help:
public static Map<String, Integer> SortByKey(Map<String, Integer> map)
{
ArrayList<String> sortedKeys =
new ArrayList<String>(map.keySet());
Collections.sort(sortedKeys);
Map<String, Integer> sortedMap = new HashMap<>();
for (String x : sortedKeys)
sortedMap.put(map.get(x), Integer.parseInt(x));
return sortedMap;
}
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.
When i try to add hashMap to another HashMap i lose old varaible. How can i fix this problem? My code is something like that.
HashMap<String, String> tmp = new HashMap<String, String>();
HashMap<String, String> map = new HashMap<String, String>();
tmp = ((HashMap<String, String>)intent.getSerializableExtra("map"));
map.putAll(tmp);
when i use this code map elements always equals tmp elements. It is not stored old elements.
Thanks for help.
HashMap<String, String> map = new HashMap<String, String>(); creates a new, empty, HashMap instance. Therefore after a call to map.putAll(tmp), your map would only contain the entries of tmp.
If map has previous entries, you shouldn't assign a new instance to this variable.
That said, even if map had previous entries, putting the entries of tmp in it would overwrite the values of all the keys that exist in both map and tmp.
Once you have updated map to not be re-initialized every time, you can avoid overwriting any existing key-value pairs in map by looping as follows:
for (String key : tmp.keySet()) {
if (!map.containsKey(key)) {
map.put(key, tmp.get(key);
}
}
I'm not sure from your question if this is exactly what you are asking, but it may be useful. Eran's suggestion (using HashMap<String, List<String>>) is also a good one if you want to store multiple Strings per key.
I have the following ArrayList
ArrayList<HashMap<String, String>> list;
HashMap<String, String> map;
with the following values inside:
list[0] = map.put("key_0", value_0);
list[1] = map.put("key_1", value_1);
list[2] = map.put("key_2", value_2);
I would like to parse the list array and get the value of the key at a specific position.
You can get the particular map from the ArrayList> by using get() method. for example,
map = list.get(index);
And to get key of that map, you can do:
String key = map.get("key");
FYI, this is the feasible solution, i dont know why you are using key name like key_0, key_1, key_2...and so on.