2

Lets say we have an object of Class Person with all String parameters :

Class Person {

 String name;
 String email;

}

Person p = new Person();
p.name = "ABC";
p.email = "abc@xyz.com";

How do we convert object p to ArrayList of NameValue Pair :

Output should be a ArrayList<NameValuePair> with following content :

"name" : "ABC" 
"email" : "abc@xyz.com"
Parth mehta
  • 1,468
  • 2
  • 23
  • 33
  • You mean name as `ABC` and value as `abc@xyz.com` – Naman Gala Nov 14 '14 at 13:06
  • You should use a hashmap if you want name value pair. Your key could be name and value could be email? Or your key could be name and your value(object) could be Person... – brso05 Nov 14 '14 at 13:07

4 Answers4

4

Here you are .. it's a dynamic so you can use it for any object .. I'm using the reflection

public static void main(String[] args) {

    Person person = new Person("Ali", "a@c.o");
    try {
        System.out.println(getObjectNameValuePairs(person));

    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

public static ArrayList<NameValuePair> getObjectNameValuePairs(Object obj) throws IllegalArgumentException, IllegalAccessException {
    ArrayList<NameValuePair> list = new ArrayList<NameValuePair>();
    for (Field field : obj.getClass().getDeclaredFields()) {
        field.setAccessible(true); // if you want to modify private fields
        NameValuePair nameValuePair = new NameValuePair();
        nameValuePair.setName(field.getName());
        nameValuePair.setValue(field.get(obj));
        list.add(nameValuePair);
    }
    return list;
}

this output will be

[[name:Ali], [email:a@c.o]]

considering that the NameValuePair implementation is

public class NameValuePair {
private String name;
private Object value;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Object getValue() {
    return value;
}

public void setValue(Object value) {
    this.value = value;
}

@Override
public String toString() {
    return "["+ name + ":" + value + "]";
}

}

I hope this could help!

Muhammad Hamed
  • 1,229
  • 9
  • 19
2

Add in Person a get method:

public NameValuePair getAsNameValuePair() {
    NameValuePair pair=new BasicNameValuePair(this.getName,this.getEmail);
    return pair;
}

If you want to store the person in an array:

public List<NameValuePair> getAsNameValuePair() {
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("name", this.getName);
    pairs.add(new BasicNameValuePair("email", this.getEmail);
    return pairs;
}

But maybe is better option to use a Map instead...

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
1

Following way you can do it.

Map<String, String> map = new HashMap<String, String>();
map.put(p.name, p.email);

or you could do like this

Map<String, Person> map = new HashMap<String, Person>();
map.put(p.name, p);
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
  • But it is recommended that name and email should be private variables and getter and setter should be exposed. – Naman Gala Nov 14 '14 at 13:12
0

You can add array list to name value pair as mentioned in the code.Just convert array list to string by .toString() method.

    kk = new ArrayList<NameValuePair>();
    kk.add(new BasicNameValuePair("task_id",task_id.toString()));
    kk.add(new BasicNameValuePair("task_status",task_status.toString()));
    kk.add(new BasicNameValuePair("task_remarks",task_remarks.toString()));
    kk.add(new BasicNameValuePair("task_rca",task_rca.toString()));


kk.add(newBasicNameValuePair("equip_serial_no",equip_serial_no.toString()));
Syed Danish Haider
  • 1,334
  • 11
  • 15