3

I want to assign all vales from hashMap to myObject:

Map <String, Object> hashMap;

UserdefinedClass myObject; 

Is there any library or direct approach available?

Artem Kulikov
  • 2,250
  • 19
  • 32
Sanal
  • 418
  • 6
  • 17

4 Answers4

0

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.

Vibhor Chopra
  • 647
  • 4
  • 14
  • what if the Hashmap is in a LinkedHashMap structure ? – Sanal Nov 03 '15 at 06:33
  • So you need to iterate LinkedHashMap to get the Hashmap, iterate Hashmap to get the Object. Rest if you can share the snippet of the code, it will be help full to give the appropriate answer. – Vibhor Chopra Nov 03 '15 at 06:46
0

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();

        }
Varun Verma
  • 542
  • 4
  • 17
0

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());
    } 
Ritt
  • 3,181
  • 3
  • 22
  • 51
0

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.

Mixaz
  • 4,068
  • 1
  • 29
  • 55