Java:如何根据给定的值映射更新一些POJO字段?

huangapple go评论92阅读模式
英文:

Java: How to update some of POJO fields according to a given map of values?

问题

以下是翻译好的内容:

我需要根据仅包含更改的映射来更新几个POJO字段。

例如(松散编码):

  1. MyClass{
  2. public MyClass(a,b,c);
  3. private int a;
  4. private int b;
  5. private int c;
  6. }
  7. int main(){
  8. Map<String, String> keyValueMap = new HashMap<>();
  9. MyClass obj = new MyClass(1,2,3);
  10. keyValueMap.put("c", 6);
  11. keyValueMap.put("b", 4);
  12. // 仅更新obj的b和c值:
  13. updateObj(obj, keyValueMap);
  14. keyValueMap.put("a", 5);
  15. keyValueMap.put("b", 7);
  16. // 仅更新obj的a和b值:
  17. updateObj(obj, keyValueMap);
  18. }
  19. void updateObj(MyClass obj, Map<String, String> keyValueMap){
  20. for (Map.Entry<String,String> entry : keyValueMap.entrySet())
  21. if(entry.getKey().equals("a")
  22. obj.setA(entry.getValue());
  23. if(entry.getKey().equals("b")
  24. obj.setB(entry.getValue());
  25. if(entry.getKey().equals("c")
  26. obj.setC(entry.getValue());
  27. }
  28. }

除了完整的if链外,是否有更好的方法来实现updateObj()吗?

谢谢大家。

英文:

I need to update several POJO fields according to a map which holds only the changes.

For example (loosely coded) :

  1. Class MyClass{
  2. public MyClass(a,b,c);
  3. private int a;
  4. private int b;
  5. private int c;
  6. }
  7. int main(){
  8. Map&lt;String, String&gt; keyValueMap = new HashMap&lt;&gt;();
  9. MyClass obj = new MyClass(1,2,3);
  10. keyValueMap.put(&quot;c&quot;, 6);
  11. keyValueMap.put(&quot;b&quot;, 4);
  12. //Update only b and c values of obj:
  13. updateObj(obj, keyValueMap);
  14. keyValueMap.put(&quot;a&quot;, 5);
  15. keyValueMap.put(&quot;b&quot;, 7);
  16. //Update only a and b values of obj:
  17. updateObj(obj, keyValueMap);
  18. }
  19. void updateObj(MyClass obj, Map&lt;String, String&gt; keyValueMap){
  20. for (Map.Entry&lt;String,String&gt; entry : keyValueMap.entrySet())
  21. if(entry.getKey().equals(&quot;a&quot;)
  22. obj.setA(entry.getValue();
  23. if(entry.getKey().equals(&quot;b&quot;)
  24. obj.setB(entry.getValue();
  25. if(entry.getKey().equals(&quot;c&quot;)
  26. obj.setC(entry.getValue();
  27. }
  28. }

Is there a better method to implement updateObj() apart of a complete if's chain?

Thank you all.

答案1

得分: 1

  1. 示例代码更新方法):
  2. static void updateObj(MyClass obj, Map<String, Integer> keyValueMap) {
  3. try {
  4. Field declaredField = null;
  5. for(String key : keyValueMap.keySet()) {
  6. declaredField = MyClass.class.getDeclaredField(key);
  7. boolean accessible = declaredField.isAccessible();
  8. declaredField.setAccessible(true);
  9. declaredField.set(obj, keyValueMap.get(key));
  10. declaredField.setAccessible(accessible);
  11. }
  12. } catch (NoSuchFieldException | IllegalAccessException e) {
  13. e.printStackTrace();
  14. }
  15. }
英文:

Example code (update method):

  1. static void updateObj(MyClass obj, Map&lt;String, Integer&gt; keyValueMap) {
  2. try {
  3. Field declaredField = null;
  4. for(String key:keyValueMap.keySet()) {
  5. declaredField = MyClass.class.getDeclaredField(key);
  6. boolean accessible = declaredField.isAccessible();
  7. declaredField.setAccessible(true);
  8. declaredField.set(obj, keyValueMap.get(key));
  9. declaredField.setAccessible(accessible);
  10. }
  11. } catch (NoSuchFieldException | IllegalAccessException e) {
  12. e.printStackTrace();
  13. }
  14. }

答案2

得分: 1

你可以使用Java的反射机制来实现你想要做的操作:

  1. static void updateObj(Main obj, Map<String, Integer> keyValueMap){
  2. try {
  3. for (Map.Entry<String, Integer> entry : keyValueMap.entrySet()) {
  4. Field field = Main.class.getField(entry.getKey());
  5. field.setAccessible(true);
  6. field.set(obj, entry.getValue());
  7. }
  8. } catch (Exception e) {}
  9. }
英文:

You can use Java reflection to do what you want to do:

  1. static void updateObj(Main obj, Map&lt;String, Integer&gt; keyValueMap){
  2. try {
  3. for (Map.Entry&lt;String,Integer&gt; entry : keyValueMap.entrySet()) {
  4. Field field = Main.class.getField(entry.getKey());
  5. field.setAccessible(true)
  6. field.set(obj,entry.getValue());
  7. }
  8. } catch(Exception e) {}
  9. }

答案3

得分: 0

你可以简单地使用反射API。以下是基于你的代码的示例:

  1. public class Main1 {
  2. public static void main(String[] args) {
  3. Map<String, String> keyValueMap = new HashMap<>();
  4. MyClass obj = new MyClass(1, 2, 3);
  5. keyValueMap.put("c", "6");
  6. keyValueMap.put("b", "4");
  7. System.out.println(obj);
  8. // 仅更新 obj 的 b 和 c 值:
  9. updateObj(obj, keyValueMap);
  10. System.out.println(obj);
  11. keyValueMap.put("a", "5");
  12. keyValueMap.put("b", "7");
  13. // 仅更新 obj 的 a 和 b 值:
  14. updateObj(obj, keyValueMap);
  15. System.out.println(obj);
  16. }
  17. private static void updateObj(MyClass myClass, Map<String, String> map) {
  18. Class<MyClass> clazz = MyClass.class;
  19. for (String fieldName : map.keySet()) {
  20. try {
  21. Field field = clazz.getDeclaredField(fieldName);
  22. field.setAccessible(true);
  23. field.set(myClass, Integer.parseInt(map.get(fieldName)));
  24. } catch (NoSuchFieldException ex) {
  25. System.out.println("找不到该字段!!!");
  26. } catch (IllegalAccessException ex) {
  27. System.out.println(ex);
  28. }
  29. }
  30. }
  31. }
  32. class MyClass {
  33. private int a;
  34. private int b;
  35. private int c;
  36. public MyClass(int a, int b, int c) {
  37. this.a = a;
  38. this.b = b;
  39. this.c = c;
  40. }
  41. @Override
  42. public String toString() {
  43. return "MyClass{" + "a=" + a + ", b=" + b + ", c=" + c + '}';
  44. }
  45. }

注意:不要忘记为私有成员使用 field.setAccessible(true);,否则会出现 IllegalAccessException。

英文:

You can simply use Reflection API.
Here is an example based on your code:

  1. public class Main1 {
  2. public static void main(String[] args) {
  3. Map&lt;String, String&gt; keyValueMap = new HashMap&lt;&gt;();
  4. MyClass obj = new MyClass(1,2,3);
  5. keyValueMap.put(&quot;c&quot;, &quot;6&quot;);
  6. keyValueMap.put(&quot;b&quot;, &quot;4&quot;);
  7. System.out.println(obj);
  8. //Update only b and c values of obj:
  9. updateObj(obj, keyValueMap);
  10. System.out.println(obj);
  11. keyValueMap.put(&quot;a&quot;, &quot;5&quot;);
  12. keyValueMap.put(&quot;b&quot;, &quot;7&quot;);
  13. //Update only a and b values of obj:
  14. updateObj(obj, keyValueMap);
  15. System.out.println(obj);
  16. }
  17. private static void updateObj(MyClass myClass, Map&lt;String, String&gt; map) {
  18. Class&lt;MyClass&gt; clazz = MyClass.class;
  19. for (String fieldName: map.keySet()) {
  20. try {
  21. Field field = clazz.getDeclaredField(fieldName);
  22. field.setAccessible(true);
  23. field.set(myClass, Integer.parseInt(map.get(fieldName)));
  24. } catch (NoSuchFieldException ex) {
  25. System.out.println(&quot;No Such Field Found!!!&quot;);
  26. } catch (IllegalAccessException ex) {
  27. System.out.println(ex);
  28. }
  29. }
  30. }
  31. }
  32. class MyClass {
  33. private int a;
  34. private int b;
  35. private int c;
  36. public MyClass(int a, int b, int c) {
  37. this.a = a;
  38. this.b = b;
  39. this.c = c;
  40. }
  41. @Override
  42. public String toString() {
  43. return &quot;MyClass{&quot; + &quot;a=&quot; + a + &quot;, b=&quot; + b + &quot;, c=&quot; + c + &#39;}&#39;;
  44. }
  45. }

Note: Do not forget to use field.setAccessible(true); for private members; Otherwise you will get an IllegalAccessException.

huangapple
  • 本文由 发表于 2020年5月5日 14:53:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61607358.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定