gpt4 book ai didi

java - 哪个更快?检查一个元素是否已经在集合中,如果没有添加它或者只是添加它?

转载 作者:行者123 更新时间:2023-12-01 07:02:58 26 4
gpt4 key购买 nike

哪种方式将元素添加到集合中最快?

在我看来,您可以通过两种不同的方式来做到这一点:

  • 检查要添加的元素是否已在集合中,如果不在集合中,则添加它。

  • 只需添加它即可,因为集合具有唯一的元素。我不确定这种方式不会引发错误。

最佳答案

直接添加即可。它有检查机制。

来自 add 方法的文档。

Adds the specified element to this set if it is not already present. More formally, adds the specified element e to this set if this set contains no element e2 such that (e==null ? e2==null : e.equals(e2)). If this set already contains the element, the call leaves the set unchanged and returns false.

关于java - 哪个更快?检查一个元素是否已经在集合中,如果没有添加它或者只是添加它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36383209/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com