我想对系统中的数据进行深层复制。我有这样的课:
Class User{
User mother;
User father;
User spouse;
}
创建单个记录后,我想像这样重建它们的引用:
Map<User, User> motherMap = new HashMap<User, User>();
Map<User, User> fatherMap = ...;
Map<User, User> spouseMap = ...;
//Now I want to populate User reference like this:
for(User user : motherMap.keySet) {
//some other similar code;
user.setMother(motherMap.get(user));
}
for(User user : fatherMap.keySet) {
//some other similar code;
user.setFather(fatherMap.get(user));
}
for(User user : motherMap.keySet) {
//some other similar code;
user.setSpouse(spouseMap.get(user));
}
知道如何清理这种类似的代码吗?我想让我的代码更好,因为在实际代码中,用户之间有超过 10 个引用。Intellij 警告这种方法分析起来太复杂了,感觉只做复制粘贴...