简体   繁体   English

Lambdaj如何工作?

[英]How does Lambdaj having work?

I am learning LambdaJ and everytime I need to achieve a task I have to check at existing examples and modify them in order to use lambdaj. 我正在学习LambdaJ,每次需要完成一项任务时,都必须检查现有示例并进行修改以使用lambdaj。

I have started using it but I think I'm missing something here that would like to ask you. 我已经开始使用它了,但我想我在这里想问您什么。

I'm not clear about using having method. 我不清楚使用having方法。 I don't understand how it works and how I can I use it. 我不了解它如何工作以及如何使用它。

I have debugged, decompiled and read its documentation but I don't find the "way of thinking" lambda. 我已经调试,反编译并阅读了其文档,但是找不到lambda的“思路”。

having javadoc says: having javadoc说:

static HasArgumentWithValue having(A argument, org.hamcrest.Matcher matcher) 静态HasArgumentWithValue具有(一个参数,org.hamcrest.Matcher匹配器)

Creates an hamcrest matcher that is evalued to true if the value of the given argument satisfies the condition defined by the passed matcher. 如果给定参数的值满足传递的匹配器定义的条件,则创建一个hamcrest匹配器,其值评估为true。

I've used having in examples like this: 我用having像这样的例子:

List<User> result = filter(having(on(User.class).getAge(), greaterThan(20)), list);

I understand that having applies a harmcrest matcher to an argument and repeats that all over the list. 据我所知, having施加harmcrest匹配了一个说法,并重申,所有在列表上。

But my question is how does having works? 但是我的问题是工作如何进行? How do I think in a functional way on lambdaj? 我如何从功能上考虑lambdaj?

The description of your code could look like this: 您的代码说明可能如下所示:

the function filter take each user from list and 
 apply it over function having 
   that retrieved the age and compare with value of 20. 

This can be wrote as 这可以写成

private List<User> filter(List<User> users) {
    final List<User> filtered = new ArrayList<>();
    for(User user : users) {
       if(having(user.getAge(),greaterThan(20)) {
          filtered.add(user);
       }
    }
    return filtered;
}


private boolean having(Integer age,  org.hamcrest.Matcher<Integer> matcher) {

   return matcher.matcher(age);
}

In other words function having take an two arguments, a value and matcher and evaluate the matcher against value. 换句话说,函数具有两个参数,即值和匹配器,并根据值评估匹配器。 And the value argument is passed by function filter that expect boolean in result. value参数由期望结果为boolean的函数过滤器传递。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM