Performance and Memory allocation comparison between List and Set

后端 未结 6 1268
执笔经年
执笔经年 2020-12-02 14:00

I want to know the comparison between List and Set in terms of performance,memory allocation and usability.

If i don\'t have any requirement of keeping the uniquenes

6条回答
  •  臣服心动
    2020-12-02 14:52

    If you don't care about the ordering, and don't delete elements, then it really boils down to whether you need to find elements in this data structure, and how fast you need those lookups to be.

    Finding an element by value in a HashSet is O(1). In an ArrayList, it's O(n).

    If you are only using the container to store a bunch of unique objects, and iterate over them at the end (in any order), then arguably ArrayList is a better choice since it's simpler and more economical.

提交回复
热议问题