这只是一个没有具体应用的理论问题.
我有以下方法,我不会碰.它可以(如果可能的话)用作BiConsumer.
void doSmallThing(A a, B b) {
// do something with a and b.
}
void doBigThing(List<A> as, B b) {
// What to do?
}
Run Code Online (Sandbox Code Playgroud)
我怎么能反覆as,同时保持b恒定,使用this::doSmallThing的doBigThing?
当然以下不起作用.
void doBigThing(List<A> as, B b) {
as.stream()
.forEach(this::doSmallThing);
}
Run Code Online (Sandbox Code Playgroud)
以下工作很好,实际上是我每天使用的.
void doBigThing(List<A> as, B b) {
as.stream()
.forEach(a -> doSmallThing(a, b));
}
Run Code Online (Sandbox Code Playgroud)
以下也适用,但有点棘手.
Consumer<A> doSmallThingWithFixedB(B b) {
return (a) -> doSmallThing(a, b);
}
void doBigThing(List<A> as, B b) {
as.stream()
.forEach(doSmallThingWithFixedB(b)) …Run Code Online (Sandbox Code Playgroud) 我想知道是否有可能获得分配给构造函数的供应商的退货类型.
例如
Supplier<Foo> sFoo = Foo::new;
Run Code Online (Sandbox Code Playgroud)
我如何从供应商处获得"Foo.class"?我一直在使用typetools来解决其他问题.
这有效,例如:
Supplier<Foo> sFoo = () -> new Foo();
Class<?> fooClasss = net.jodah.typetools.TypeResolver.resolveRawArguments(Supplier.class, sFoo.getClass())[0];
// fooClass == Foo.class
Run Code Online (Sandbox Code Playgroud)
但是,如果我分配如下的供应商:Supplier<Foo> sFoo = Foo::new,则无法解析退货类型......
有什么想法吗?我没有必要使用typetools btw ...
关于本地类构造函数的方法引用,有几个类似的问题,但我想稍微澄清一下其他的东西.请考虑以下代码:
static Callable gen(int i) {
class X {
int x = i;
public String toString() { return "" + x; }
}
return X::new;
}
...
System.out.println(gen(0).call());
System.out.println(gen(1).call());
Run Code Online (Sandbox Code Playgroud)
显然这将打印输出
0
1
Run Code Online (Sandbox Code Playgroud)
事实证明,X该类具有表单的构造函数...$X(int)(您可以通过它找到它X.class.getDeclaredConstructors()).
但有趣的是,返回的lambdas(或方法引用)并不是对构造函数的简单引用...$X(int),例如,Integer::new.它们在内部...$X(int)使用预定义的参数(0或1)调用此构造函数.
所以,我不确定,但看起来这种方法参考在JLS中没有精确描述.除了本地类的这种情况之外,没有其他方法可以生成这种lambda(使用预定义的构造函数参数).谁能帮忙澄清一下?
确切地说:
JLS在哪里描述了这种方法参考?
是否有任何其他方法来创建此类方法引用具有预定义参数的任意类构造函数?
在我给我公司的讲座中,我建议将任何复杂的lambda转换为方法参考(更可读,更好的调试和测试),并被问到是否总是可行的.
我搜索并找不到无法用方法引用替换的lambda.
我对吗?(lambda总是可以用方法引用替换)
在这个Java类中:
import java.util.function.*;
public class T {
public String func(String a) {
System.out.println("There we go: " + a);
return a;
}
public static void main(String... args) {
final Supplier<T> c = T::new;
final BiFunction<T, String, String> f = T::func;
final T t = c.get();
final String v = f.apply(t, "something");
System.out.println(v);
}
}
Run Code Online (Sandbox Code Playgroud)
我可以获得T对实例方法的构造函数的方法引用func.
有没有办法在scala中做同样的事情,即得到
val c: () => T = ??? // default constructor of T as function
val f: (T, String) => String = …Run Code Online (Sandbox Code Playgroud) 您如何精确链接Java 8实例的方法引用?例:
Collections.sort(civs,Comparator.comparing(Civilization::getStrategy.getStrategLevel));
Run Code Online (Sandbox Code Playgroud)
getStrategy一个的Civilization实例返回一个Strategy具有实例方法的对象实例getStrategyLevel。
为什么该Comparator.comparing方法不返回带有由lambda表达式实现的功能接口的比较器?
在下面的类中,我将方法引用WordCounterEx::accumulate作为第二个参数传递给reduce方法.reduce方法的签名是:
<U> U reduce(U identity,
BiFunction<U, ? super T, U> accumulator,
BinaryOperator<U> combiner);
Run Code Online (Sandbox Code Playgroud)
因此reduce方法的第二个参数必须满足BiFunction配方.但是传递的累积方法不是BiFunction(它只有一个参数).为什么还要编译?
public class WordCounterEx {
private final int counter;
private final boolean lastSpace;
public WordCounterEx(int counter, boolean lastSpace) {
this.counter = counter;
this.lastSpace = lastSpace;
}
public int countWords(Stream<Character> stream) {
WordCounterEx wordCounter = stream.reduce(new WordCounterEx(0, true),
//HOW CAN THIS WORK? here must come BiFunction - R apply(T t, U u);
WordCounterEx::accumulate,
WordCounterEx::combine);
return wordCounter.counter;
}
public WordCounterEx accumulate(Character c) {
if(Character.isWhitespace(c)) {
return lastSpace …Run Code Online (Sandbox Code Playgroud) public static void main(String o[]) {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 1);
map.entrySet().stream().sorted(Comparator.comparing(Entry::getValue)).forEach(System.out::println);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码构建和运行完美但不应该.Comparator.comparing采用函数引用,只有那些接受一个参数并返回一个参数的方法才能映射到此.但是在上面的代码中,getValue被映射并且工作正常但它不带任何参数.代码应该给出构建问题,但不是.我的概念有什么问题吗?
我有两个简单的代码:
IntFunction<String> f1 = Integer::toString;
Function<Integer, String> f2 = Integer::toString;
Run Code Online (Sandbox Code Playgroud)
我认为这两个定义都是正确的,并且等价相同,但第二个定义了编译错误,抱怨说 Required Function<Integer, String>,but Method Reference is found.
我试图理解方法引用如何在java中工作.乍一看,它非常简单.但是当谈到这样的事情时:
Foo类中有一个方法:
public class Foo {
public Foo merge(Foo another) {
//some logic
}
}
Run Code Online (Sandbox Code Playgroud)
在另一个类Bar中有一个这样的方法:
public class Bar {
public void function(BiFunction<Foo, Foo, Foo> biFunction) {
//some logic
}
}
Run Code Online (Sandbox Code Playgroud)
并使用方法参考:
new Bar().function(Foo::merge);
Run Code Online (Sandbox Code Playgroud)
它符合并且有效,但我不明白它是如何匹配的:
Foo merge(Foo another)
Run Code Online (Sandbox Code Playgroud)
到BiFunction方法:
R apply(T t, U u);
Run Code Online (Sandbox Code Playgroud)
???
method-reference ×10
java ×9
java-8 ×9
lambda ×5
comparator ×1
function ×1
java-stream ×1
methods ×1
reflection ×1
scala ×1