I want to assign all vales from hashMap
to myObject
:
Map <String, Object> hashMap;
UserdefinedClass myObject;
Is there any library or direct approach available?
I want to assign all vales from hashMap
to myObject
:
Map <String, Object> hashMap;
UserdefinedClass myObject;
Is there any library or direct approach available?
Step 1: Add values to Object(Entity class). Step 2: Add Object to Hashmap as a value wrt Key. Step 3: Get the Object from HasMap and use iterator to iterate the values.
Iterator iterator = meMap.keySet().iterator();
while(iterator.hasNext()) {
String key=(String)iterator.next();
String value=(String)meMap.get(key);
}
Fell free to ask for any query.
You can loop over the hash map and then do whatever you want...
Iterator<Entry<String,Object>> iterator = hashMap.entrySet().iterator();
while(iterator.hasNext()){
set = iterator.next();
Object val = set.getValue();
}
Let'assume this your Hashmap
Map <String, Object> hashMap = new HashMap<String,Object>();
And this is your class object where you want to assign the values from hashmap.
List<UserdefinedClass> myObject = new ArrayList<UserdefinedClass>();
Now, you need to iterate through the hashmap and get the value, type cast it and store it in myObject list.
Iterator it = hashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry)it.next();
myObject.add((UserdefinedClass)pair.getValue());
}
Have a look at Convert a Map to a POJO
Jackson
can be used in android project for sure. BeanUtils
are told to be much faster, but I have no experience with it on Android. High chances are that it can be used as well.