将多个源 ArrayList 同步到单个目标列表中 [英] Sync multiple source ArrayLists into single target list

查看:25
本文介绍了将多个源 ArrayList 同步到单个目标列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要从两个不同的表中加载项目列表(ArrayList).由于复杂的 Hibernate 映射,它们不能作为一个 ArrayList 加载.因此它们在类定义中由两个不同的 Hibernate 包分别加载.当我使用它们时,我想合并成一个列表.所以我在考虑写一个函数如下,

There is a requirement to load a list of items(ArrayList) from two different tables. Due to complex Hibernate mapping, they can not be loaded as one ArrayList. So they are loaded separately by two different Hibernate bags in the class definition. When I use them I'd like to combine into single list. So I am thinking about writing a function as follows,

private List<Item> combinedList = new ArrayList<Item>();

public List<Item> getCombinedList()
{
   combinedList.clear();
   combinedList.addAll(getList1());
   combinedList.addAll(getList2());
   return combinedList;
}

getCombinedList() 将用于显示项目并用于其他只读操作.任何删除或修改或添加操作仍将在 List1 和 List2 中执行(因为它将由 Hibernate 处理).

getCombinedList() will be used to display the items and used in other read-only operations. Any delete or modify or add operations will still be executed in List1 and List2 (Because it will be handled by Hibernate).

我的问题是如何在发生更改时有效地将 getCombinedList() 与源列表同步?

My question is how can I efficiently sync getCombinedList() with source lists when there is a change?

更新:每次调用 getCombinedList() 以获取最新项目效率低下.我正在寻找一种方法来同步底层数据结构 combinedList.

Update: Calling getCombinedList() every time to get the latest items is inefficient. I am looking for a way to sync the underlying data structure combinedList.

推荐答案

您必须编写自己的列表实现,该列表将使用两个列表进行初始化,并将所有方法执行委托给它们两个以确保突变操作.

You'll have to write your own implementation of a list that will be initialized with the two lists and delegate all method execution to both of them securing mutation operations.

这篇关于将多个源 ArrayList 同步到单个目标列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆