Java collection insertion: Set vs. List

后端 未结 7 908
花落未央
花落未央 2021-02-14 10:56

I\'m thinking about filling a collection with a large amount of unique objects. How is the cost of an insert in a Set (say HashSet) compared to an List (say ArrayList)?

7条回答
  •  悲&欢浪女
    2021-02-14 11:40

    You're right: set structures are inherently more complex in order to recognize and eliminate duplicates. Whether this overhead is significant for your case should be tested with a benchmark.

    Another factor is memory usage. If your objects are very small, the memory overhead introduced by the set structure can be significant. In the most extreme case (TreeSet vs. ArrayList) the set structure can require more than 10 times as much memory.

提交回复
热议问题