简体   繁体   English

我可以结合使用Java8(流,lambda)和番石榴吗?

[英]can i combine java8 (stream, lambda) and guava?

When dealing with transformations(eg: transform List<People> people to List<Integer> ages , where People is a class that contains a property age ), normally there are 2 ways (for me) of doing this: 在处理转换时(例如:将List<People> peopleList<Integer> ages ,其中People是包含属性age的类),通常有两种方法(对我而言):

  1. use java8: 使用java8:

    people.stream().map(p -> p.getAge()).collect(toList()) ; people.stream().map(p -> p.getAge()).collect(toList()) ;

  2. or user guava2: 或用户guava2:

    Lists2.transform(people, People2AgeTransformer.INSTANCE); where the People2AgeTransformer is a transformer that implements the Function interface to return the age. 其中People2AgeTransformer是实现了Function接口以返回年龄的转换器。

Recently, i accidentlly found that java8 and guava can be combined and therefore the code can be : 最近,我偶然发现java8和guava可以结合使用,因此代码可以是:

Lists2.transform(people, p->p.getAge());

this piece of code complies and runs OK without any error, which get me confused. 这段代码遵循并运行正常,没有任何错误,这让我感到困惑。 The method Lists2.transform() requires the 2nd args to be the implementation of interface com.google.common.base.Function , while the java8 lambda is actually the implementation of interface java.util.function.Function .(Well, they both declares a method B apply(A input) to transform A to B.) 方法Lists2.transform()要求第二个args是接口com.google.common.base.Function的实现,而java8 lambda实际上是接口java.util.function.Function的实现。(嗯,它们都声明方法B apply(A input)将A转换为B。)

I don't understand why this would work since they are two different interface of different package. 我不明白为什么这行得通,因为它们是不同包的两个不同接口。

the java8 lambda is actually the implementation of interface java.util.function.Function java8 lambda实际上是接口java.util.function.Function的实现

No it's not. 不,这不对。 p -> p.getAge() does not have a predefined type - its type is inferred at compile time depending on the context in which it's called. p -> p.getAge()没有预定义的类型-它的类型是在编译时根据调用它的上下文推断出来的。 Any functional interface that has a int/Integer getAge(Person p) method will work. 任何具有int/Integer getAge(Person p)方法的功能接口都可以使用。

So both java.util.Function<Person, Integer> and com.google.common.base.Function<Person, Integer> are compatible. 因此, java.util.Function<Person, Integer>com.google.common.base.Function<Person, Integer>都是兼容的。

If you have a function assigned to variable or passed via parameter, it's really easy to adapt them via Java-8 method references: 如果您有一个分配给变量或通过参数传递的函数,那么很容易通过Java-8方法引用对其进行调整:

java.util.function.Function<Person, Integer> javaFunction = p -> p.getAge();
com.google.common.base.Function<Person, Integer> guavaFunction = javaFunction::apply;
java.util.function.Function<Person, Integer> javaFunction2 = guavaFunction::apply;

So if you already have Java 8 Function object in variable fn and need to pass it into some Guava code, just use fn::apply . 因此,如果您已经在变量fn具有Java 8 Function对象,并且需要将其传递给一些Guava代码,则只需使用fn::apply If you have Java 8 Predicate in variable pred , use pred::test when passing it into Guava code. 如果您在变量pred包含Java 8 Predicate ,则在将其传递到Guava代码时使用pred::test Similar method references could be used for other functional interfaces. 类似的方法引用可用于其他功能接口。

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

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