Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用lambdaj从嵌套数组中提取对象_Java_Nested Loops_Lambdaj - Fatal编程技术网

Java 使用lambdaj从嵌套数组中提取对象

Java 使用lambdaj从嵌套数组中提取对象,java,nested-loops,lambdaj,Java,Nested Loops,Lambdaj,我想用lambdaj从嵌套数组中提取一个对象。 我的模型是拥有“元素”数组的“产品”列表: 在代码的某个地方,我有一个产品列表,我想在列表中找到一个具有特定代码的元素 根据以下讨论:,我可以使用: select(myproductList, having(on(Product.class).getElements() .contains(selectUnique(elements, having(on(Ele

我想用lambdaj从嵌套数组中提取一个对象。 我的模型是拥有“元素”数组的“产品”列表:

在代码的某个地方,我有一个产品列表,我想在列表中找到一个具有特定代码的元素

根据以下讨论:,我可以使用:

select(myproductList,
       having(on(Product.class).getElements()
                .contains(selectUnique(elements, 
                    having(on(Element.class).getCode(), equalTo("codeToFind"))));
但不幸的是,这将无法编译,因为
getElements()
是一个数组而不是集合

因此,我以以下java代码结束:

 for (Product p : products) {
    for (Element e : p.getElements()) {
       if (e.getCode().equals("codeTofind")) {
           return e;
       }
    }
 }
 return null;
有没有办法用lambdaJ遍历嵌套数组?

好的,我找到了一个解决方案:

selectFirst(flatten(extract(products, on(Product.class).getElements())),
                          having(on(Element.class).getCode(), equalTo("mycode")));
这将首先选择并展平唯一集合中的所有my元素,然后在code属性上对其进行过滤

从性能角度看,我不确定这是否是最好的解决方案:在完成完整扫描之前,似乎所有产品和元素都已展平。(我对Lambdaj的理解太弱,无法确定)

我认为完整的java实现更有效,因为它在匹配第一个代码时停止

selectFirst(flatten(extract(products, on(Product.class).getElements())),
                          having(on(Element.class).getCode(), equalTo("mycode")));