Performance and Memory allocation comparison between List and Set

后端 未结 6 1252
执笔经年
执笔经年 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:40

    If you don't have the requirement to have unique elements in collection simply use ArrayList unless you have very specific needs.

    If you have the requirement to have only unique elemets in collection, then use HashSet unless you have very specific needs.

    Concerning SortedSet (and it's implementor TreeSet), as per JavaDoc:

    A Set that further provides a total ordering on its elements. The elements are ordered using their natural ordering, or by a Comparator typically provided at sorted set creation time.

    Meaning it's targeted at quite specific use cases, when elements should be always ordered in a set, which is not needed usually.

提交回复
热议问题