gpt4 book ai didi

java - 获取任意java方法的委托(delegate)函数

转载 作者:行者123 更新时间:2023-12-01 22:29:27 24 4
gpt4 key购买 nike

有没有一种简单的方法可以获取 POJO 上方法的任意 Function 版本?

例如:

FluentIterable.from(myCollection).uniqueIndex(Functions.for(Item.class).getId)
.first(Predicates.equalTo(id)).get();

Functions.for 的理想行为类似于 Mockito.mock

最佳答案

其实你可以使用lambdaj这是

a library that makes easier to address this issue by allowing to manipulate collections in a pseudo-functional and statically typed way.

Lambdaj 具有与 Mockito 类似的技巧,因此您应该阅读其 limitations (最重要的是您的 POJO 一定不是最终的)。

使用 lambdaj,您的代码可能是这样的(请注意,示例中的 uniqueIndex 返回 Map,它没有 first 方法,所以我在这里猜测):

import ch.lambdaj.Lambda.*; // for all static methods used below

// just items indexed by their ids
Map<Intgeger, Item> indexed = index(myCollection, on(Item.class).getId());
// or more likely you want
Item foundItem = selectFirst(
myCollection, having(on(Item.class).getId(), equalTo(id)));

关于java - 获取任意java方法的委托(delegate)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28113919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com