when to use Set vs. Collection?

后端 未结 8 1901
遥遥无期
遥遥无期 2020-12-24 10:55

Is there any practical difference between a Set and Collection in Java, besides the fact that a Collection can include the same elemen

相关标签:
8条回答
  • 2020-12-24 11:48

    As Collection is a super type of Set and SortedSet these can be passed to a method which expects a Collection. Collection just means it may or may not be sorted, order or allow duplicates.

    0 讨论(0)
  • 2020-12-24 11:49

    You should use a Set when that is what you want.

    For example, a List without any order or duplicates. Methods like contains are quite useful.

    A collection is much more generic. I believe that what mmyers wrote on their usage says it all.

    0 讨论(0)
提交回复
热议问题