在哪里可以找到 Guava 的基本谓词(例如,大于)?

发布于 2024-10-28 14:30:19 字数 265 浏览 3 评论 0原文

I am using the guava library and have noticed that a very useful Predicate is not defined - "greater than". Is there another place I should be looking for basic predicates like this, or am I doomed to create my own functional support jar that includes things like this, and import it into all of my projects? Is there a reason they wouldn't include this,but would take the time to do a bunch of other predicates (In the Predicates class)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

分开我的手 2024-11-04 14:30:19

范围Ranges(更新:从 Guava 14.0 开始,Ranges 上的静态方法已合并到 Range 中)现已为 r10 添加。您将能够执行以下操作:

Iterable<Integer> positive = Iterables.filter(numbers, Range.greaterThan(0));

Range 具有许多其他强大的功能,包括将 Range 查看为连续的 ImmutableSortedSet 的能力> 在离散域上:

ContiguousSet<Integer> oneToOneHundred = ContiguousSet.create(
    Range.closed(1, 100), DiscreteDomains.integers());

我刚刚在这里展示了Integer,但是Range内容适用于任何ComparableContigeousSet 需要 DiscreteDomain 类型... Guava 提供了 DiscreteDomain.integers().longs() 和目前.bigIntegers()

Range and Ranges (update: the static methods on Ranges have been folded into Range as of Guava 14.0) have now been added for r10. You'll be able to just do:

Iterable<Integer> positive = Iterables.filter(numbers, Range.greaterThan(0));

Ranges have a lot of other powerful functionality, including the ability to view a Range as a contiguous ImmutableSortedSet over a discrete domain:

ContiguousSet<Integer> oneToOneHundred = ContiguousSet.create(
    Range.closed(1, 100), DiscreteDomains.integers());

I just showed Integers here, but the Range stuff works for any Comparable. ContiguousSet requires a DiscreteDomain for the type... Guava provides DiscreteDomain.integers(), .longs() and .bigIntegers() at the moment.

断桥再见 2024-11-04 14:30:19

借助 Predicate 接口和使用谓词过滤集合的各种实用方法,Guava 提供了您可以构建的核心。

Predicates 类允许您创建一些常用的谓词。我想您可以按照 Mike 的建议在问题跟踪器中提出增强请求,但我不确定他们会添加它,因为 Guava 力求高功率重量比。

如果他们要添加“greaterThan”谓词,他们还需要添加“greaterOrEqualThan”、“lesserThan”、“lesserOrEqualThan”...这会很有用,但是对于这样的谓词来说,这是很多“API 膨胀”只需一行即可实现。不过值得一试。

更好的解决方案可能是拥有一个开源项目,该项目可以使用 Guava 本身不提供的所有“不错的”功能来扩展 Guava。我们可以将其称为“guava-leftovers”或其他名称;)或者可以要求 Biscotti 项目添加这样的实用方法(它们已经具有一些 Guava 中没有的“不错的”功能)。

With the Predicate interface and the various utility methods to filter collections with a Predicate, Guava provides a core you can build upon.

The Predicates class lets you create some commonly used predicates. I guess you could do a request for enhancement in the issue tracker, as suggested by Mike, but I'm not sure they would add it, since Guava strives for a high power-to-weight ratio.

If they were to add the "greaterThan" predicate, they would also need to add "greaterOrEqualThan", "lesserThan", "lesserOrEqualThan"... This would be useful, but this is a lot of "API bloat" for a Predicate that only takes one line to implement. Worth a try, though.

A better solution might be to have an open-source project that extends Guava with all the "nice-to-have" functionality that is not available in Guava proper. We could call it "guava-leftovers" or something ;) Or maybe ask the Biscotti project to add such utility methods (they already have some "nice-to-have" functionality that's not in Guava).

帅的被狗咬 2024-11-04 14:30:19

我之前请求过此功能并被提及此问题。显然,这个功能将通过 Ranges 来实现,Range 将实现 Predicate。

I have previously requested this functionality and been referred to this issue. Apparently this functionality will be implemented through Ranges, which will implement Predicate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文