合并`putIfAbsent`和`compute`为一行代码。

huangapple go评论70阅读模式
英文:

Merging putIfAbsent followed by compute to one line

问题

我有一种感觉,以下代码可以更加简洁:

changeMap.putIfAbsent(before, new Tuple(-1, -1));
changeMap.putIfAbsent(after, new Tuple(-1, -1));
changeMap.compute(before, (integer, tuple) -> new Tuple(index, tuple.right));
changeMap.compute(after, (integer, tuple) -> new Tuple(tuple.left, index));

有什么建议吗?

英文:

I have a feeling that the following code can be made little more concise:

changeMap.putIfAbsent(before, new Tuple(-1, -1));
changeMap.putIfAbsent(after, new Tuple(-1, -1));
changeMap.compute(before, (integer, tuple) -> new Tuple(index, tuple.right));
changeMap.compute(after, (integer, tuple) -> new Tuple(tuple.left, index));

Any pointers?

答案1

得分: 1

你可以在Map上使用合并函数。

英文:

You can use the merge function on Map

huangapple
  • 本文由 发表于 2020年8月8日 20:00:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63315127.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定