【发布时间】:2015-12-01 14:49:32
【问题描述】:
我的意思是,目前我正在开发 T 类型元素的两个排序集合之间的合并(只要您提供一种比较类型的方法,类型并不重要,例如,在 Java 中,A Comparator<T> 将做这项工作)。
我不希望必须合并合并过程中涉及的两个数据结构(我不希望获得一个包含两个元素合并的全新结构)。我想要的是有某种合并过程的观察者,以便定义如何处理另一个类中的每个合并元素。例如,a 想要这样的东西:
merger.merge(leftCollection,rightCollection,theComparator,theObserver).
观察者是一个观察合并算法并收到动作通知的对象,我的意思是:
interface MergeObserver<T> {
/**
* Triggered when the merge algorithm decides to merge only the left entry.
* This case correspond to the case when there is no equivalent entry on the right collection.
*/
public void mergeLeft(T entry);
/**
* Triggered when the merge algorithm decides to merge both entries.
* This case correspond to the case when there exists the same entry on both collections.
*/
public void mergeBoth(T left, T right);
/**
* Triggered when the merge algorithm decides to merge only the right entry.
* This case correspond to the case when there is no equivalent entry on the left collection.
*/
public void mergeRight(T entry);
}
我已经实现了排序集合的实现,但是......我想分享这种感觉,问题来了,如果有人以前想过这个,特别是在 Guava 库中,什么是正确的使用的术语。
我认为这是问这个问题的正确地方。对现有解决方案的任何想法将不胜感激。非常感谢你。不要删除我的感谢。 谢谢。
【问题讨论】:
-
我怀疑这对于通用库来说太特殊了,因为 MergeObserver 抽象仅在合并(或以其他方式连接)两个数据集时有用,这在企业计算中通常由数据库完成,很少这样做。
-
您是否使用比较器始终将来自
left的值与来自right的值进行比较?如果是这种情况,请使用您自己的比较器包装给定的比较器,它只是委托比较,然后根据比较结果通知观察者(<0用于mergeLeft,==0用于mergeBoth和@ 987654330@mergeRight)。
标签: java merge guava terminology