两个类找出不同属性值

类,找不同

起因

公司用的 hibernate,表结构更新时也需要更新对应的 pojo。因为更新结构,基本是新增列属性。同时原有的 pojo 中会有相对应的注解标识,为了提高方便性,只需增加新增的属性值即可。

解决方案

因为人工查看字段容易出错,所以这边采用了反射来进行处理。

public class Demo {
    public static void main(String[] args) {
        Field[] aFields = A.class.getDeclaredFields();
        Field[] bFields = B.class.getDeclaredFields();

        Field[] fields = Arrays.stream(bFields).filter(bField ->
                Arrays.stream(aFields).allMatch(afield ->
                    !afield.getName().equals(bField.getName()))
        ).toArray(Field[]::new);
    }
}
posted @ 2022-03-17 10:36  平安QAQ  阅读(25)  评论(0编辑  收藏  举报