I need to update several POJO fields according to a map which holds only the changes.
For example (loosely coded):
Class MyClass{
public MyClass(a,b,c);
private int a;
private int b;
private int c;
}
int main(){
Map<String, String> keyValueMap = new HashMap<>();
MyClass obj = new MyClass(1,2,3);
keyValueMap.put("c", 6);
keyValueMap.put("b", 4);
//Update only b and c values of obj:
updateObj(obj, keyValueMap);
keyValueMap.put("a", 5);
keyValueMap.put("b", 7);
//Update only a and b values of obj:
updateObj(obj, keyValueMap);
}
void updateObj(MyClass obj, Map<String, String> keyValueMap){
for (Map.Entry<String,String> entry : keyValueMap.entrySet())
if(entry.getKey().equals("a")
obj.setA(entry.getValue();
if(entry.getKey().equals("b")
obj.setB(entry.getValue();
if(entry.getKey().equals("c")
obj.setC(entry.getValue();
}
}
Is there a better method to implement updateObj() apart of a complete if's chain?
Thank you all.
Example code (update method):
static void updateObj(MyClass obj, Map<String, Integer> keyValueMap) {
try {
Field declaredField = null;
for(String key:keyValueMap.keySet()) {
declaredField = MyClass.class.getDeclaredField(key);
boolean accessible = declaredField.isAccessible();
declaredField.setAccessible(true);
declaredField.set(obj, keyValueMap.get(key));
declaredField.setAccessible(accessible);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
You can use Java reflection to do what you want to do:
static void updateObj(Main obj, Map<String, Integer> keyValueMap){
try {
for (Map.Entry<String,Integer> entry : keyValueMap.entrySet()) {
Field field = Main.class.getField(entry.getKey());
field.setAccessible(true)
field.set(obj,entry.getValue());
}
} catch(Exception e) {}
}
You can simply use Reflection API. Here is an example based on your code:
public class Main1 {
public static void main(String[] args) {
Map<String, String> keyValueMap = new HashMap<>();
MyClass obj = new MyClass(1,2,3);
keyValueMap.put("c", "6");
keyValueMap.put("b", "4");
System.out.println(obj);
//Update only b and c values of obj:
updateObj(obj, keyValueMap);
System.out.println(obj);
keyValueMap.put("a", "5");
keyValueMap.put("b", "7");
//Update only a and b values of obj:
updateObj(obj, keyValueMap);
System.out.println(obj);
}
private static void updateObj(MyClass myClass, Map<String, String> map) {
Class<MyClass> clazz = MyClass.class;
for (String fieldName: map.keySet()) {
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
field.set(myClass, Integer.parseInt(map.get(fieldName)));
} catch (NoSuchFieldException ex) {
System.out.println("No Such Field Found!!!");
} catch (IllegalAccessException ex) {
System.out.println(ex);
}
}
}
}
class MyClass {
private int a;
private int b;
private int c;
public MyClass(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
@Override
public String toString() {
return "MyClass{" + "a=" + a + ", b=" + b + ", c=" + c + '}';
}
}
Note: Do not forget to use field.setAccessible(true);
for private members; Otherwise you will get an IllegalAccessException.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.