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 = "[email protected]";

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

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

"name" : "ABC" 
"email" : "[email protected]"
2
  • You mean name as ABC and value as [email protected] Commented Nov 14, 2014 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... Commented Nov 14, 2014 at 13:07

4 Answers 4

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", "[email protected]");
    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:[email protected]]]

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!

Sign up to request clarification or add additional context in comments.

Comments

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...

Comments

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

1 Comment

But it is recommended that name and email should be private variables and getter and setter should be exposed.
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()));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.