【发布时间】:2019-11-20 09:29:01
【问题描述】:
我有三个列表,在第三个列表中我正在合并两个列表。如果我在第三个中进行更改,为什么更改会反映到其他列表以及如何克服?
List<Communication> communicationFromList = fromTicket.getCommunications();
List<Communication> communicationToList = toTicket.getCommunications();
List<Communication> mergedCommunication=new ArrayList<>();
mergedCommunication.addAll(communicationToList);
mergedCommunication.addAll(communicationFromList);
for (int i = index; i < mergedCommunication.size(); i++) {
if (!ObjectUtils.isEmpty(mergedCommunication.get(i))) {
int j =i;
Communication communication = mergedCommunication.get(i);
communication.setCommSeqId(++j);
communication.setMergeInfo("Merged From: " + fromTicketId);
}
}
由于上述更改也会反映到其他列表中。如何克服
【问题讨论】:
-
您正在更改
Communication对象,而不是列表。
标签: java spring spring-boot collections java-8