我试图了解JDK下游减少的实现.就这个:
public static <T, K, D, A, M extends Map<K, D>>
Collector<T, ?, M> groupingBy(Function<? super T, ? extends K> classifier,
Supplier<M> mapFactory,
Collector<? super T, A, D> downstream) {
Supplier<A> downstreamSupplier = downstream.supplier();
BiConsumer<A, ? super T> downstreamAccumulator = downstream.accumulator();
BiConsumer<Map<K, A>, T> accumulator = (m, t) -> {
K key = Objects.requireNonNull(classifier.apply(t), "element cannot be mapped to a null key");
A container = m.computeIfAbsent(key, k -> downstreamSupplier.get());
downstreamAccumulator.accept(container, t);
};
BinaryOperator<Map<K, A>> merger = Collectors.<K, A, Map<K, A>>mapMerger(downstream.combiner()); …Run Code Online (Sandbox Code Playgroud) 我有以下非常大的(~10e8记录)表(table):
+--------------------------------+ | id order value | +--------------------------------+ | PK int int | | 1 1 1 | | 2 2 5 | | 3 2 | | 4 2 0 | +--------------------------------+
如您所见,value列只能包含非负整数或null.现在,我需要编写一个返回没有a的订单的查询value > 0(即order = 2不保存条件,因为有记录value = 5).
该反向查询很简单:
SELECT order
FROM table
WHERE value > 0
Run Code Online (Sandbox Code Playgroud)
查询的性能对我来说是令人满意的.
但我们不能写
SELECT order
FROM table
WHERE value = 0
Run Code Online (Sandbox Code Playgroud)
因为有可能有一个具有相同订单的记录,但有value > 0.我能找到写这个查询的唯一方法是:
SELECT order
FROM table
GROUP BY …Run Code Online (Sandbox Code Playgroud) 我正在阅读 Scott Meyrse C++,现在我正在阅读有关封装的部分。他说除非将数据成员声明为私有,否则无法封装数据成员。这很清楚。
但是由于我来自Java,拥有它的package-private方法和成员,我很感兴趣的是 C++ 是否允许我们做一些技巧来在命名空间中声明一些,以便在命名空间之外无法访问它。命名空间私有或类似的东西。我认为使用匿名命名空间的以下代码会很好:
namespace A {
namespace { //anonymous namespace within the namespace
int a;
}
void foo(){ std::cout << a << std::endl; }
}
int main()
{
A::a = 2;
A::foo();
}
Run Code Online (Sandbox Code Playgroud)
但效果很好:http : //coliru.stacked-crooked.com/a/b4690b9bb28dad29
我正在阅读Scott Meyers的Effective C++,而我正在阅读关于继承的部分.他说过
纯虚函数仅指定接口的继承.
简单(不纯)虚函数指定接口的继承加上默认实现的继承.
现在,考虑以下两个类:
struct A {
virtual void foo() = 0;
};
void A::foo(){ std::cout << "Default foo" << std::endl; }
struct B : A{
virtual void foo(){ A::foo(); std::cout << "Derived foo" << std::endl; }
};
Run Code Online (Sandbox Code Playgroud)
我们仍然可以像在示例中那样为纯虚函数提供默认实现并通过它调用它qualified-function-call-expression.对于不纯的虚函数,我们几乎可以做同样的事情
struct A {
virtual void foo(); //No longer pure virtual
};
void A::foo(){ std::cout << "Default foo" << std::endl; }
struct B : A{
virtual void foo(){ A::foo(); std::cout << "Derived foo" << std::endl; } …Run Code Online (Sandbox Code Playgroud) c++ inheritance virtual-functions definition one-definition-rule
我试着用Java中的Streams来计算单词.这是我试过的:
public static int countWords(String s) {
return s.chars().reduce((x, y) -> {
if((char)y == ' ')
++x;
return x;
}).orElse(0);
}
Run Code Online (Sandbox Code Playgroud)
但是countWords("asd")回归97.为什么?我认为实际上由s 组成的chars回报.所以,我只是把它投到了.怎么了?IntStreamcharchar
我正在阅读Scott Meyerses C++,现在正在阅读有关管理资源的部分.他解释说,shared-ptr是一个引用计数智能指针,它就像一个垃圾收集器,除了它不能破坏引用循环.这是什么意思?什么是引用的破坏周期?
我正在阅读Ricahrd Warburton的Java 8书,他提供了以下练习:
尝试使用方法引用重写以下内容:
[...]
用于连接列表的flatMap方法
我真的不明白如何在flatMap这里申请.令我感到困惑的是,平面地图用于将a的每个元素映射Stream到另一个Stream,然后将它们连接在一起以产生更大的Stream但是在这里我们必须分开List<T>.
public static <T> List<T> concat(List<T> lst1, List<T> lst2){
//lst1.stream().flatMap() - it maps each elements
//of lst1 to stream and concatenates it for each
//element
}
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我已经看过这个答案了。
的行为让我有点困惑BigDecimal.valueOf(float)。这是示例:
System.out.println(BigDecimal.valueOf(20.2f)); //prints 20.200000762939453
Run Code Online (Sandbox Code Playgroud)
所以
float f = BigDecimal.valueOf(20.2f)
.setScale(2, RoundingMode.UP)
.floatValue();
System.out.println(f); //prints 20.21
Run Code Online (Sandbox Code Playgroud)
这绝对不是我预期的行为。有没有办法round up正确浮动避免此类错误?
我float和之间的区别感到困惑double.我读过这篇文章.我认为差异只在于精确度.所以我期望如果0.1 + 0.2 == 0.3返回false那么0.1f + 0.2f == 0.3f.
但实际上是0.1f + 0.2f == 0.3f回归true.为什么?
这是纯粹的Java问题还是什么?
我正在阅读Richard Warburton的Java 8书.这是我不太明白的引用:
让我们假设流框架正在将我们的工作拆分为在四核机器上并行运行:
我们的数据源被分解为四个元素块.
我们在每个线程上并行执行叶子计算工作 [...]
什么是叶子计算工作?它应该是什么意思?
java ×7
java-8 ×4
c++ ×3
collectors ×1
definition ×1
inheritance ×1
java-stream ×1
namespaces ×1
postgresql ×1
sql ×1