jeudi 17 mars 2016

Java - Convert list of variables to map of variable name to variable value

Suppose I have a few variables:

public void foo() {
    String name = "Bob";
    String gender = "Male";
    Integer age = 6;
    String address = "some address";
}

How can I turn them into a Map<String, Object> like

{
    "name": "Bob",
    "gender": "Male",
    "age": 6,
    "address": "some address",
}

other than manually inserting them into the map. Can this be done using Reflection?





Aucun commentaire:

Enregistrer un commentaire