同步多个源的ArrayList到单一的目标清单 [英] Sync multiple source ArrayLists into single target list

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

问题描述

有是加载从两个不同的表项(ArrayList的)的列表的要求。由于复杂的Hibernate映射,它们不能被装载为一体的ArrayList。因此,他们是由两个不同的休眠袋类定义单独加载。当我使用他们,我想结合成单一的名单。因此,我想编写一个函数如下,

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中和列表2(因为它会由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天全站免登陆