Java 8最有用的功能之一是default接口上的新方法.基本上有两个原因(可能还有其他原因)为什么会被引入:
Iterator.remove()Iterable.forEach()从API设计者的角度来看,我希望能够在接口方法上使用其他修饰符,例如final.在添加便捷方法时,这将非常有用,可防止在实现类时出现"意外"覆盖:
interface Sender {
// Convenience method to send an empty message
default final void send() {
send(null);
}
// Implementations should only implement this method
void send(String message);
}
Run Code Online (Sandbox Code Playgroud)
如果Sender是一个类,上面已经是常见的做法:
abstract class Sender {
// Convenience method to send an empty message
final void send() {
send(null);
}
// Implementations should only implement this method
abstract void send(String message);
}
Run Code Online (Sandbox Code Playgroud)
现在,default并final有明显矛盾的关键字,但默认关键字本身不会一直严格要求 …
对于我的具体情况,我想减少使用功能组合;例如:
BiFunction<ImmutableSet<Integer>, ImmutableSet<Integer>, Sets.SetView<Integer>> f = Sets::intersection;
Function<Sets.SetView<Integer>, ImmutableSet<Integer>> g = Sets.SetView::immutableCopy;
BiFunction<ImmutableSet<Integer>, ImmutableSet<Integer>, ImmutableSet<Integer>> biFunction = f.andThen(g);
ImmutableSet<Integer> intersection = Stream.of(ImmutableSet.of(1, 2, 3), ImmutableSet.of(1, 2), ImmutableSet.of(4))
.reduce(biFunction)
.orElse(ImmutableSet.of());
Run Code Online (Sandbox Code Playgroud)
这有一个编译错误:
BiFunction<ImmutableSet<Integer>, ImmutableSet<Integer>, Sets.SetView<Integer>> f = Sets::intersection;
Function<Sets.SetView<Integer>, ImmutableSet<Integer>> g = Sets.SetView::immutableCopy;
BiFunction<ImmutableSet<Integer>, ImmutableSet<Integer>, ImmutableSet<Integer>> biFunction = f.andThen(g);
ImmutableSet<Integer> intersection = Stream.of(ImmutableSet.of(1, 2, 3), ImmutableSet.of(1, 2), ImmutableSet.of(4))
.reduce(biFunction)
.orElse(ImmutableSet.of());
Run Code Online (Sandbox Code Playgroud)
相反,我需要这样做:
ImmutableSet<Integer> intersection = Stream.of(ImmutableSet.of(1, 2, 3), ImmutableSet.of(1, 2), ImmutableSet.of(4))
.reduce((a, b) -> Sets.intersection(a, b).immutableCopy())
.orElse(ImmutableSet.of());
Run Code Online (Sandbox Code Playgroud)
然而,这失去了组合提供的无点风格。
为什么 Stream API …
以下签名在Scala中有效且常用:
trait Collection[A] {
def reduceLeft [B >: A] (f: (B, A) => B): B
}
Run Code Online (Sandbox Code Playgroud)
但是,由于Java中>:的Scala是等效的super,我转换此签名的第一个想法(用函数类型替换BiFunction并使用Use-Site variance annotations aka Bounded Wildcards)将是
interface Collection<A> {
<B super A> B reduceLeft(BiFunction<? super B, ? super A, ? extends B> mapper)
}
Run Code Online (Sandbox Code Playgroud)
但是哦,不!编译器抱怨super令牌,<B super A>因为你不能有低边界的类型变量!现在,如何在Java代码中编写此方法而不必回顾Java世界中不存在泛型的时间?
是的,我知道你认为我可以使用B extends A,但这不是一回事,正如我的实现所示:
public <R extends E> R reduceLeft(BiFunction<? super R, ? super E, ? extends R> mapper)
{
if (this.isEmpty())
{
return …Run Code Online (Sandbox Code Playgroud) 为什么方法中的累加器参数是Stream::reducea BiFunction而不是BinaryOperator像combiner参数一样.
为什么是它的类型 BiFunction<U, ? super T, U>?为什么T?应该是BiFunction<U, ? extends U, U>吗?