【问题标题】:Comparing objects using hamcrest and lambdaj based on multiple inputs基于多个输入使用 hamcrest 和 lambdaj 比较对象
【发布时间】:2012-12-02 15:15:58
【问题描述】:

我正在尝试过滤数组列表以删除与用户输入的一组条件不匹配的对象。该对象有 3 个列表。例如,请参见下文。

public class A {
    public String [] a={"a", "b", "c"};
    public String [] c={"a", "b", "c"};
    public String [] d={"a", "b", "c"};
}

我正在尝试编写一种搜索此对象的方法,以查看它是否与输入到搜索对话框中的数据匹配。例如,我想要在数组 a 中具有“a”、在数组 b 中具有“c”和在数组 c 中具有 * 的所有对象。

    for(int i = 0; i < products.length; i++)
    {
        reactionData = select(reactionData, having(on(A.class).a,Every.everyItem(containsString(products[i]))));
    }

我想为我搜索的每个数组做这样的事情,但它似乎效率低下。更不用说在每次搜索时都会替换 reactData 数组。

如果有人可以帮助我解决这个问题,或者可能指向一些有用的网站来解释如何将 hamcrest 与 lambdaj 一起使用,那将是理想的。

编辑

output = filter(having(on(Reaction.class).getChemicalProducts(), hasItems(products)), reactionData);

上述匹配器必须匹配产品中的每个项目。我正在寻找它来匹配产品中的任何项目

【问题讨论】:

  • 输出=过滤器(有(on(Reaction.class).getChemicalProducts(),hasItems(产品)),reactionData);

标签: java arrays search hamcrest lambdaj


【解决方案1】:

以下行将保留products 列表中至少包含一种化学产品的所有实例:

output = filter(having(on(Reaction.class).getChemicalProducts(), hasItem(isIn(products))), reactionData);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-04
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多