英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论