如何使用Java中的lambda表达式按列表比较两个列表的大小

时间:2021-01-22 18:02:25

I am a complete newbie using lambda expression and I can't sort this simple task:

我是一个使用lambda表达式的完全新手,我无法对这个简单的任务进行排序:

List<String> a = new ArrayList<>();
a.add("1");
a.add("potato");
a.add("John");
a.add("0");

List<String> b = new ArrayList<>();
a.add("Monday");
a.add("123#");
a.add("abc");
a.add("Smith");
a.add("Hello");
a.add("test101001");

So I want to check if a.size() is equal to b.size(). It should return false. I need to use a lambda expression.

所以我想检查a.size()是否等于b.size()。它应该返回false。我需要使用lambda表达式。

2 个解决方案

#1


2  

Not sure if it's what you need, but you can use BiPredicate for this:

不确定它是否是您需要的,但您可以使用BiPredicate:

BiPredicate<List<String>, List<String>> predicate = (l1, l2) -> l1.size() == l2.size();


boolean test = predicate.test(a, b);

#2


1  

The lambda is easy:

lambda很简单:

(List<String> l1, List<String> l2) -> {
  return l1.size() == l2.size();
}

For example. But the real point is: a lambda doesn't make sense on it is own. It is tool that you use to solve specific problems.

例如。但真正的观点是:一个lambda对它来说没有意义。它是用于解决特定问题的工具。

#1


2  

Not sure if it's what you need, but you can use BiPredicate for this:

不确定它是否是您需要的,但您可以使用BiPredicate:

BiPredicate<List<String>, List<String>> predicate = (l1, l2) -> l1.size() == l2.size();


boolean test = predicate.test(a, b);

#2


1  

The lambda is easy:

lambda很简单:

(List<String> l1, List<String> l2) -> {
  return l1.size() == l2.size();
}

For example. But the real point is: a lambda doesn't make sense on it is own. It is tool that you use to solve specific problems.

例如。但真正的观点是:一个lambda对它来说没有意义。它是用于解决特定问题的工具。