stream api比較簡單
函數式接口的設計 不是很好理解,記一個復雜的情況
@FunctionalInterface
public interface ProductSpec {
boolean satisfy(Long p);
// 此處的p 是satisfy的參數p
static ProductSpec color(BigDecimal decimal) {
return p -> p.longValue() == decimal.longValue();
}
}
public void testSatisfy(ProductSpec productSpec) {
productSpec.satisfy(111L);
}
public void test() {
BigDecimal bigDecimal = new BigDecimal(12);
// 第一種
testSatisfy(new ProductSpec() {
@Override
public boolean satisfy(Long p) {
return p.longValue() == bigDecimal.longValue();
}
});
// 第二種
testSatisfy(p -> p.longValue() == bigDecimal.longValue());
// 第三種
testSatisfy(ProductSpec.color(bigDecimal));
}
如上 第三種一開始確實不好理解。。。但是代碼卻可以高復用,相比較簡介