我对Java 8中的以下构造感到好奇:
double[] doubles = //...
double sum = DoubleStream.of(doubles).parallel().sum();
Run Code Online (Sandbox Code Playgroud)
切入追逐:
sum
始终相同,例如在不同的计算机上运行时?更多背景......
浮点运算是有损的,并且(与实值运算不同)不是关联的.因此,除非注意工作如何分割和重新组合,否则可能导致不确定的结果.
我很高兴地发现这种sum()
方法采用了Kahan Summation.这显着减少了错误,但仍未提供精确的*结果.
在我的测试中,重复调用似乎每次返回相同的结果,但我想知道我们可以安全地假设它是多么稳定.例如:
我很高兴在每台计算机上采用相同的JVM版本.
这是我掀起的一项测试:
public static void main(String[] args) {
Random random = new Random(42L);
for (int j = 1; j < 20; j++) {
// Stream increases in size and the magnitude of the values at each iteration.
double[] doubles = generate(random, j*100, j);
// Like a simple for loop
double sum1 = DoubleStream.of(doubles).reduce(0, …
Run Code Online (Sandbox Code Playgroud) 我发现自己想要一个Collectors.toMap
返回的变体ImmutableMap
,这样我就能做到:
ImmutableMap result = list.stream().collect(MyCollectors.toImmutableMap(
tuple -> tuple._1(), tuple -> tuple._2());
Run Code Online (Sandbox Code Playgroud)
(tuple
在这个特定的例子中是Scala Tuple2
)
我刚刚了解到这样的方法将在Guava 21中使用Java-8支持(耶!)但这听起来好一个月之后.有谁知道今天可能实现的任何现有库(等)?
ImmutableMap
并非严格要求,但似乎是我要求的最佳选择:按键查找,并保留原始迭代顺序.不变性也是首选.
请注意,这FluentIterable.toMap(Function)
还不够,因为我既需要键映射功能,也需要值映射功能.
我试图在主人之上重新分支一个分支,这是我以前做过的一千次.但今天它没有用:
> git status
On branch mystuff
Your branch and 'master' have diverged,
and have 6 and 2 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
nothing to commit, working directory clean
> git rebase
First, rewinding head to replay your work on top of it...
> git status
On branch mystuff
Your branch is up-to-date with 'master'.
Untracked files:
(use "git add <file>..." to include in what will be committed)
[a directory from …
Run Code Online (Sandbox Code Playgroud) 好的'JOptionPane包含了大量的静态方法.有许多组合,但要更改某些选项(如按钮),您仍然必须指定其他可选参数 - 通常是默认值(如空图标).这不会导致易于阅读的代码.
此外,方法不是特别一致(int
返回对应于***_OPTION
常量或按钮索引?)因此需要大量文档来消除歧义:它不易于学习,记忆或写作.
对我来说,创建一个'Builder'包装器似乎很自然.它可能看起来像这样:
String[] buttonText = { "Looks good", "It sucks" };
Object selection = new OptionPaneBuilder("What do you think?")
.question()
.message(messageComponent)
.resizable(true)
.showOptionDialog(parent, buttonText);
return buttonText[0].equals(selection);
Run Code Online (Sandbox Code Playgroud)
最后的方法调用可能是:
// returns int (or enum?)
.showConfirmDialog(parent, JOptionPane.YES_NO_CANCEL_OPTION)
// returns JOptionPane
.build()
// etc...
Run Code Online (Sandbox Code Playgroud)
我很乐意去写它 - 但是我没有找到任何现有的东西让我想知道:我是疯了(这是一个坏主意/有更好的方法)......或者只是无能为力?:-)
那么......有没有人知道任何可以达到这样的东西?
我的感觉是坚持使用JOptionPane对话框,以便UI代表获得字体和系统图标等正确的外观和感觉; 但我想如果替代方案在这方面取得成功,他们也会没事的.
如果这是重复的,我深表歉意,但我不知道这是一个 git“功能”还是 zsh,但是如果我切换到某个地方,我有大量已删除的删除分支。
我已经尝试过了prune
,但这似乎不是我想要的。
我在本地唯一的分支机构是:
master
development
fix/root-cleanup
Run Code Online (Sandbox Code Playgroud)