有自定义Comparators的retainAll()的实现吗? [英] Is there a implementation of retainAll() with custom Comparators?

查看:417
本文介绍了有自定义Comparators的retainAll()的实现吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据其他收藏集的内容来筛选收藏集。通常,我会使用 Collection.retainAll()方法。


不幸的是,我处理的域对象的等同性是相当不稳定的,这取决于比较上下文。因此,我不能依赖 equals / hashCode

我的下一个反射是使用自定义的 Comparator ,但我找不到任何支持我的想法的方法。实现功能不是一个问题,但我觉得在这里重新发明轮子。

我错过了API中的一些东西吗?
如果没有,什么优化会使我的直接impl(通过遍历n ^ 2中的两个集合的所有项目创建一个列表的所有对象在两个集合)一个好的?

I need to sift Collections according to contents of other Collections. Usually, I would have used the Collection.retainAll() method.

Unfortunately, the equality of the domain objects I am handling is rather volatile, depending on the comparison context. Thus, I cannot rely on equals/hashCode.

My next reflex was to use a custom Comparator, but I was unable to find any methods supporting what I had in mind. Implementing the functionality is not a problem but I feel like reinventing the wheel here.

Have I missed something in the API? Other frameworks (not too esoteric) like commons are welcome too.
And if not, what optimizations would make my straightforward impl (creating a list of all Objects inside both Collections by going over all items of both Collections in n^2) a good one?

推荐答案

建议使用 Guava的过滤器。另一种方法是滚动您自己的 retainAllBy(sourceCollection,Comparator)

Suggest using Guava's filter. Alternative is to roll your own retainAllBy(sourceCollection, Comparator).

这篇关于有自定义Comparators的retainAll()的实现吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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