我正在学习写一些lambda表示作为FunctionalInterface.所以,要添加我使用的两个整数:
BiFunction<Integer, Integer, Integer> biFunction = (a, b) -> a + b;
System.out.println(biFunction.apply(10, 60));
Run Code Online (Sandbox Code Playgroud)
给我输出70.但如果我这样写的话
BinaryOperator<Integer, Integer, Integer> binaryOperator = (a, b) -> a + b;
Run Code Online (Sandbox Code Playgroud)
我收到一个错误说
错误的类型参数数量:3; 要求:1
不是BinaryOperator孩子BinaryFunction吗?我该如何改进?
例如,
List<Product> productsList = new ArrayList<Product>();
productsList.add(new Product(1,"HP Laptop",25000f));
productsList.add(new Product(2,"Dell Laptop",30000f));
productsList.add(new Product(3,"Lenevo Laptop",28000f));
productsList.add(new Product(4,"Sony Laptop",28000f));
productsList.add(new Product(5,"Apple Laptop",90000f));
Float totalPrice = productsList.stream()
.map(product->product.price)
.reduce(0.0f,(sum, price)->sum+price);
System.out.println(totalPrice);
Run Code Online (Sandbox Code Playgroud)
这是哪个功能界面,(sum, price)->sum+price是指?
我有两种生产“消费者”的工厂方法使用不同的方法 lambda 和方法引用:
@SuppressWarnings("Convert2MethodRef")
public Consumer<String> lambdaPrintStringConsumer(){
return x -> System.out.println(x);
}
public Consumer<String> methodRefPrintStringConsumer(){
return System.out::println;
}
Run Code Online (Sandbox Code Playgroud)
我发现在第一种情况 ( lambdaPrintStringConsumer()) 中,方法返回对同一对象的引用
@Test
public void shouldSameFromFactoryMethod_lambda() {
Consumer<String> consumerA = lambdaPrintStringConsumer();
Consumer<String> consumerB = lambdaPrintStringConsumer();
Assert.assertSame(consumerA, consumerB);//consumerA == consumerB --> true
}
Run Code Online (Sandbox Code Playgroud)
但是在第二个 ( methodRefPrintStringConsumer()) 中,对象是不同的
@Test
public void shouldNotSameFromFactoryMethod_methodRef() {
Consumer<String> consumerA = methodRefPrintStringConsumer();
Consumer<String> consumerB = methodRefPrintStringConsumer();
Assert.assertNotSame(consumerA, consumerB);//consumerA == consumerB --> false
}
Run Code Online (Sandbox Code Playgroud)
直接方法返回与以下相同的结果shouldNotSameFromFactoryMethod_methodRef():
@SuppressWarnings("Convert2MethodRef")
@Test
public void shouldNotSameFromLambda() {
Consumer<String> …Run Code Online (Sandbox Code Playgroud) 此功能即将推出Kotlin 1.4。这是摘录自KotlinConf'19.
fun interface Action {
fun run()
}
fun runAction(a: Action) = a.run()
runAction{
println("Hello")
}
Run Code Online (Sandbox Code Playgroud)
看起来不错,但我仍然不知道它有什么作用。
什么是函数接口?它的实用价值是什么?它可以用于哪些具体场景?
我正在探索Java 8的功能,并遇到了"功能接口".
根据我的理解,这些接口可以有一些默认的实现方法:
@FunctionalInterface
public interface ComplexFunctionalInterface extends SimpleFuncInterface
{
default public void doSomeWork()
{
System.out.println("Doing some work in interface impl...");
}
default public void doSomeOtherWork()
{
System.out.println("Doing some other work in interface impl...");
}
}
Run Code Online (Sandbox Code Playgroud)
但我怀疑的是,这是抽象类的用途.
为什么要介绍的功能接口.
如果我编写了ToIntFunction接口,我想在接口中编码它只是一个返回原始int的函数,如下所示:
@FunctionalInterface
public interface ToIntFunction<T> extends Function<T, Integer> {
int applyAsInt(T value);
@Override
default Integer apply(T value) {
return Integer.valueOf(applyAsInt(value));
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道,有没有令人信服的理由让Java 8 API设计人员选择将原始备选方案与Function完全分开?是否有一些证据表明他们认为这样做并决定反对呢?我想类似的问题至少包括其他一些"特殊"功能接口,如Consumer(可能是Function <T,Void>)和Supplier(Function <Void,T>).
我没有深入和彻底地考虑过这个问题的所有后果,所以我可能会遗漏一些东西.
如果ToIntFunction(和其他原始泛型函数接口)与Function有这种关系,它将允许一个人在预期使用Function参数的位置使用它(想到的是与其他函数的组合,例如调用myFunction.compose(myIntFunction))或者当在如上所述的这种自动(非)装箱实现就足够时避免在API中编写几个专用函数.
这与这个问题非常相似:为什么Java 8的Predicate <T>不扩展Function <T,Boolean>但我已经意识到答案可能因语义原因而不同.因此,我正在重新设计这个函数的简单原始替代方案的问题,其中不存在任何语义,只是原始与包装类型,甚至消除了空包装对象的可能性.
我正在尝试使用lambdas,Java但无法理解它是如何工作的.我这样创建@FunctionalInterface:
@FunctionalInterface
public interface MyFunctionalInterface {
String getString(String s);
}
Run Code Online (Sandbox Code Playgroud)
现在在我的代码中我使用lambdaas在这里:
MyFunctionalInterface function = (f) -> {
Date d = new Date();
return d.toString() + " " + person.name + " used fnc str";
};
Run Code Online (Sandbox Code Playgroud)
接下来,我想利用我function将它传递给另一个类的构造函数并像这样使用它:
public SampleClass(MyFunctionalInterface function) {
String tmp = "The person info: %s";
this.result = String.format(tmp, function.getString(String.valueOf(function)));
}
Run Code Online (Sandbox Code Playgroud)
为什么我需要在valueOf()这里使用它?我认为,谢谢你,我可以使用function.getString()吗?
输出:
Tue Sep 19 11:04:48 CEST 2017 John used fnc str
我有一些如下所示的代码:
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
class MyObj {
private final Double aDouble;
public MyObj(Double aDouble) {
this.aDouble = aDouble;
}
}
class Main {
public static void main(String[] args) {
List<Function<MyObj, String>> functionList1 = new ArrayList<>();
List<Function<MyObj, String>> functionList2 = new ArrayList<>();
// ... Add same Function<MyObj, String>s to both lists
// I want to assert that functionList1.equals functionList2
}
}
Run Code Online (Sandbox Code Playgroud)
我想确认一些Function,Supplier,BiFunction或不管它可能是MyObj如果将等于另一个调用的结果Function/Supplier等返回给定相同的输入相同的值。
因此,在这种情况下,Java 将使用equalslike …
so I do have code like this:
public ConsumerTestClass(Consumer<String> consumer) {
}
public static void printString(String text) {
System.out.println(text);
}
Run Code Online (Sandbox Code Playgroud)
And from the method of other class, I would like to create object of ConsumerTestClass:
new ConsumerTestClass(/*passing consumer here*/);
Run Code Online (Sandbox Code Playgroud)
And as a consumer I would like to pass ConsumerTestClass::printString, but to be able to do that I need to pass argument as well, so it looks like that: (text) -> ConsumerTestClass.printString(text). And my question is... Is it …
我想将函数式 kotlin 接口(具有单个抽象方法的接口)实现为 kotlin lambda。怎么做?
\n\nKotlin 接口
\n\n@FunctionalInterface\ninterface Foo{\n fun bar(input: String): String \n}\nRun Code Online (Sandbox Code Playgroud)\n\nKotlin 实现。
\n\nfun createFoo(): Foo {\n return { input: String -> "Hello $input!" }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\xe2\x86\x91 无法编译 \xe2\x86\x91
\n\n它必须作为对象来实现,这非常丑陋。
\n\nfun createFoo() = \n object : Foo{\n override fun bar(input: String)= "Hello $input"\n }\nRun Code Online (Sandbox Code Playgroud)\n\n编辑:将我的示例界面从 java 更正为 kotlin
\n