我有一个像这样的Java-8-FunctionalInterface:
@FunctionalInterface
public interface A {
void doIt ();
}
Run Code Online (Sandbox Code Playgroud)
该Function-Interface提供的compose-方法.我想用它来减少这样的流A:
Stream<A> as;
A composed = as.reduce (() -> {}, Function::compose);
Run Code Online (Sandbox Code Playgroud)
结果我希望有一个函数A,它调用每个AStream的方法doIt.
composed.doIt (); // Executes every doIt ()
Run Code Online (Sandbox Code Playgroud)
但由于A 不是实现者Function,因此Function::compose不可能使用方法参考.我不能从Function(或Supplier)扩展,因为那时我将有两个抽象方法(我自己和来自Function).
我能做些什么来使我的功能成为可能A呢?
import org.junit.Test;
import java.util.stream.IntStream;
public class GomanTest {
@Test
public void someTest() {
IntStream.of(2, 3, 1).collect(Container::new, Container::add, null);
}
}
class Container<T> {
void add(T t) {
System.out.println("this is container " + t);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
this is container 2
this is container 3
this is container 1
Run Code Online (Sandbox Code Playgroud)
这在1.8.0_45.jdk上成功运行.Container#add如何被翻译成ObjIntConsumer #accept?
我正在尝试编写一个方法,该方法接受Runnable类的构造函数并根据输入的构造函数以某种方式运行它.
所以我希望能够做到这样的事情:
executeInParallel(someRunnable::new)
executeInParallel(someOtherRunnable::new)
Run Code Online (Sandbox Code Playgroud)
我的问题是如何定义executeInParallel方法,以便能够传递我在参数中定义的任何Runnable构造函数?基本上我的问题是为了做到这一点我该如何定义这个方法?
void executeInParallel(??){ ... }
看来我只能将函数接口的方法作为参数,所以我不能executeInParallel用一个接受多个xRunnable::new构造函数的参数来定义我是否有办法在不使用某种工厂的情况下执行此操作图案?
我想说明我想要这样做的原因是我想传递构造函数而不是实例.我不能在executeInParallel之外生成实例,它必须在该方法内生成.我还想传递不同的构造函数,这些构造函数采用不同的参数
先感谢您
编辑 对不起,我希望这个问题更加清晰.
例如,我有这个接口:
package tester;
public interface Calculator{
public int calculate(int a,int b);
}
Run Code Online (Sandbox Code Playgroud)
要么
package tester;
@FunctionalInterface
public interface Calculator{
public int calculate(int a,int b);
}
Run Code Online (Sandbox Code Playgroud)
我也可以将第一个视为功能界面.例如 :
package tester;
public class A {
int a;
int b;
int sum;
A(Calculator c,int e, int d){
a=e;
b=d;
sum =c.calculate(a, b);
}
int get(){
return sum;
}
}
Run Code Online (Sandbox Code Playgroud)
班长
package tester;
public class runner {
public static void main(String a[]){
System.out.println(new A((x,b)-> (x+b),1,2).get());
}
}
Run Code Online (Sandbox Code Playgroud)
代码可以使用或不使用注释,那么为什么注释呢?为什么不能说任何具有单一方法的接口都可以作为功能接口?
我想了解有关Java 8 FunctionalInterface注释的更多信息.我编写了以下代码作为实验,但它没有编译:
@FunctionalInterface
public interface HasToString {
String toString();
}
Run Code Online (Sandbox Code Playgroud)
找不到目标方法
有趣的是,这确实编译:
@FunctionalInterface
public interface HasToString {
String notToString();
}
Run Code Online (Sandbox Code Playgroud)
为什么是这样?
在Java中,可以编写如下代码:
model.getObservableProduct().observe(this, new Observer<ProductEntity>() {
@Override
public void onChanged(@Nullable ProductEntity productEntity) {
model.setProduct(productEntity);
}
});
Run Code Online (Sandbox Code Playgroud)
问题:是否可以覆盖Kotlin中的本地功能?
我有一个基本的SpringBoot应用程序.使用Spring Initializer,JPA,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行JAR文件我想创建一个基于POJO的自定义SummaryStatistics,它有2个字段,price和updateDate.此统计数据应该是最小/最大价格和最小/最大价格的日期.
public class MenuPriceSummaryStatistics implements Consumer<MenuPrice> {
private long count;
private double sum;
private double sumCompensation; // Low order bits of sum
private double simpleSum; // Used to compute right sum for non-finite inputs
private double min = Double.POSITIVE_INFINITY;
private double max = Double.NEGATIVE_INFINITY;
private Date maxDate;
private Date minDate;
public MenuPriceSummaryStatistics() {
}
/**
* Combines the state of another {@code DoubleSummaryStatistics} into this one.
*
* @param other
* another {@code DoubleSummaryStatistics}
* @throws NullPointerException
* if …Run Code Online (Sandbox Code Playgroud) lambda functional-programming java-8 java-stream functional-interface
假设我们的方法接收输入 String并返回一些List output。此输出是一些生成器的结果,其中一些生成器取决于输入,而有些则不取决于输入-它们只是添加预定义的值。我想将这些生成器实现为一些函数接口的列表(例如,Consumer),然后将它们组合到一个Consumer中,然后将其应用于输入String。
因此,我将能够轻松,独立地更换小型发电机。但是问题在于,并非我的所有生成器都需要输入 String作为参数,而我只是出于一个原因就将这个参数传递给了它-能够将此类使用者与其他使用者组合在一起。
public class ConsumersTest {
private static <T, U> BiConsumer<T, U> combine(List<BiConsumer<T, U>> consumers) {
return consumers.stream().reduce((arg1, arg2) -> {}, BiConsumer::andThen);
}
List<String> generate(String input) {
ArrayList<String> output = new ArrayList<>();
combine(getGenerators()).accept(input, output);
return output;
}
private List<BiConsumer<String, List<String>>> getGenerators() {
return Arrays.asList(
this::addFirstDependent,
this::addSecondIndependent
);
}
private void addFirstDependent(String input, List<String> output) {
if (input.contains("some string")) {
output.add("First-Dependent");
}
}
private void addSecondIndependent(String input, List<String> output) {
output.add("Predefined Output");
}}
Run Code Online (Sandbox Code Playgroud)
是否可以将不同的消费者合并到一个保护伞下并在一个地方应用?还是这是一个坏主意,而不是正确的方法?
我了解,如果所引用的方法与功能接口具有相同数量的args并返回相同的类型,则可以使用方法引用来实现功能接口,但是为什么在某些情况下,所引用的方法与功能接口具有不同数量的args但仍然兼容吗?
我有一个简单的BiConsumer,我尝试使用方法引用来实现它。我知道只要args的数量匹配,我也可以使用lambda表达式。我将显示代码以清楚地说明它。
我有一个BiConsumer<ArrayList<String>, ? super String>我要实现的。
Lambda表达式的实现方式是:
BiConsumer<ArrayList<String>, ? super String> b = (firstArg,secondArg) -> firstArg.add(secondArg); 由于它们都带有2个输入参数,因此没有问题。
但是为什么BiConsumer<ArrayList<String>, ? super String> a = ArrayList::add;也兼容?addArrayList上的方法仅需要1个输入args,而功能接口则需要2个。
任何答案将不胜感激。谢谢!
考虑以下示例:
public class LambdaArgsTest {
private static void display(Supplier<?> arg) {
try {
// this is the place where the Exception("wrong") might be thrown
// and it is in fact handled
System.out.println(arg.get());
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
display(() -> {
if(/*some condition*/) {
// this statement will be rejected due to unhandled exception
throw new Exception("wrong");
}
return "abcde";
});
}
}
Run Code Online (Sandbox Code Playgroud)
问题来了:上面例子中的 lambda 参数是一个稍后将在“display()”方法中执行的对象。将参数传递给“display()”时显然不会执行它。
为什么会被编译器拒绝?我认为用 try...catch 包围它是很合理的,只有当 lambda 被实际调用时。