一种机制,用于在Collection中的对象上具有不同的equals(物理equals和logical equals) [英] A Mechanism for having different equals (physical equals and logical equals) on objects in Collection

查看:154
本文介绍了一种机制,用于在Collection中的对象上具有不同的equals(物理equals和logical equals)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有Equalator机制像Comparator,所以我可以有不同的等于共享列表?



编辑:我的目标是区分当前列表1.equals(list2),它检查其浅表副本或深复制与所有对象a.equals(b)和list1.identical(list2),它检查其简单浅复制与未修改列表



所有这些列表都来自同一个模型。有些是自己的副本,所以他们持有指向同一对象的指针,而其他是深层副本,因此层次结构是完全复制的,因为它们在内容更新,而不仅仅是结构。



我发现自己经常makin list1.equals(list2),但我需要一个机制,告诉是否都是TOTAL副本(相同的对象以相同的顺序为集合),有时如果他们是逻辑副本(通过我自己实现的逻辑等于)所以列表将调用equals和对象应该实现的东西比a == b。



我的问题是没有Equalator接口,如果我覆盖对象等于我松比较能力为TOTAL EQUAL(a == b)



例如,这将是很好的;

  Collections.equal(l1,l2,new Equalator(){
@Override public boolean equals(Obj1,Obj2){
//默认列表比较基于
的对象return(obj1.propertyX()== obj2.propertyX());
}
});

,我仍然可以做list1.equals(list2),所以他们使用默认等于(obj1 == obj2 )



第一个操作对于检查列表是否有用是非常有用的(这可能是一个更新的列表,其中包含完全重新创建的对象模型)仍然等于旧列表。



第二个操作对于检查列表是否有用(它是数据模型的旧的当前版本的浅拷贝),它不包含移动它的任何超越的改变里面的代码,当它是udpdated版本。



编辑:一个很好的例子是有一个Point(x,y)列表。我们应该能够知道两个列表是否相等,因为它们是完全相同的点集合或相等,因为它们包含的点在逻辑方面是相等的。如果我们可以实现phyEqual和logEqual对象,并且在任何对象中有两个方法,那么list.phyEqual(list2)或list1.logEqual(list2)

解决方案

晚回答,但也许对某人有用...



Guava Equivalence 类与用于比较的等效方法相同。您需要编写自己的方法来比较列表(在Guava中没有支持),但是您可以使用各种等价定义来调用此方法。



或者您可以滚动自己的界面:

  interface Equalator< T& {
boolean equals(T o1,T o2);
}

同样,你需要写你的(trivial)方法

  boolean< T> listEquals(List< T> list1,List< T> list2,Equalator< T> equalator){
...
}


...但是您可以将它与不同的ListEqualator实现重用。


Is there any Equalator mechanism like Comparator so I can have different equals for coparing lists?

EDIT: My goal is to differentiate between current list1.equals(list2) which checks if its a shallow copy or also a deep copy with all objects a.equals(b) and list1.identical(list2) which checks if its simply shallow copy with unmodified listing

All these lists are from the same model. Some are copies of themselves so they hold the pointer to same objects, and others are deep copies so hierarchy is totally replicated, because they have updates in content, not just in structure.

I find myself oftenly makin list1.equals(list2) but I need a mechanism for telling if both are TOTAL copies (same objects in same order for collections) or sometimes if they are LOGICAL copies (through my own implemented logic equals), so list would call equals and objects should implement something more than a==b.

My problem is there is no Equalator interface, and if I override objects equals to I loose the capability of comparing by TOTAL EQUAL (a==b)

For example, this would be nice;

Collections.equal(l1,l2,new Equalator(){
    @Override public boolean equals(Obj1,Obj2){
          //Default lists comparison plus commparison of objects based on
          return (obj1.propertyX() == obj2.propertyX());
    }
});

and still I could do list1.equals(list2) so they use default equals (obj1==obj2) and this would be only true if contained objects are exactly the same.

First operation is useful for checking if list (which could be an updated list with totally recreated objects from the model) is still equals to the old list.

Second operation is useful for checking if list (which was a shallow copy of the old current version of data model), it does not contain any transcendent change from moving it around inside the code when it was the udpdated version.

EDIT: A very good example would be having a list of Point(x,y). We should be able to know if both list are equal because they are exactly same set of points or equal because the points they contain are equal in a logical way. If we could implement both phyEqual and logEqual to object, and have both methods in any object so list.phyEqual(list2) or list1.logEqual(list2)

解决方案

A late answer, but maybe it will be useful for someone...

The Guava Equivalence class is the same for equivalence as Comparator for comparing. You would need to write your own method for comparing the lists (there is no support in Guava for that), but then you could call this method with various equivalence-definitions.

Or you can roll your own interface:

interface Equalator<T> {
    boolean equals(T o1, T o2);
}

Again, you need to write your (trivial) method

boolean <T> listEquals(List<T> list1, List<T> list2, Equalator<T> equalator) {
    ...  
}

...but then you can reuse it with different ListEqualator implementations.

这篇关于一种机制,用于在Collection中的对象上具有不同的equals(物理equals和logical equals)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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