java 中的可读键值对数据结构

Readable key value pair data-structure in java

我有一个键值数组

String[] fields      = {"firstName", "middleName", "id"};
String[] fieldValues = {"first name", "middle name", "student id"};

我有一个比较两个 bean 和 return 具有不同字段值的字符串数组的方法。

 public static String[] beanCompare(Object A, Object B, 
                                          String[] fields, String[] fieldValue);

示例如果我传递一个具有不同名字和 ID 的 Studentbean。 它将 return

["first name", "student id"].

我需要比较 100 颗豆子。
return 值更新 activity 日志 table。假设 firstName 字段已更新。我们显示

the first name has been updated

在UI。是用来审核的 有没有一种可读和可维护的方式来表示这样的键值对?

如果我错了请纠正我,但似乎(以这种方式传递字段名称和字段值)您正在尝试动态构建自定义对象。

如果这是你的目的,那么你就完全 XY problem, since you've figured out this solution to your problem, and then you're trying to tune up this solution, while you should go back to the problem, which has a standard, engineered, best practice solution: the Builder Pattern

如果对象上有很多字段,但在某些情况下只需要使用其中的一部分,而在其他情况下需要使用另一部分,那么请使用 Builder,设置只有您需要的字段,然后调用 build() 方法,并获取您的对象,而无需执行您正在做的事情或置换所有构造函数。

Here is an example Java 中的此模式。

所以,枚举对我有用。

    public enum FieldEnum {

    firstName("first name"), middleName("middle name"), id("student id");

    String value;

    FieldEnum(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }
    }

它更具可读性和可维护性。