Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when to use Set vs. Collection?

Is there any practical difference between a Set and Collection in Java, besides the fact that a Collection can include the same element twice? They have the same methods.

(For example, does Set give me more options to use libraries which accept Sets but not Collections?)

edit: I can think of at least 5 different situations to judge this question. Can anyone else come up with more? I want to make sure I understand the subtleties here.

  1. designing a method which accepts an argument of Set or Collection. Collection is more general and accepts more possibilities of input. (if I'm designing a specific class or interface, I'm being nicer to my consumers and stricter on my subclassers/implementers if I use Collection.)
  2. designing a method which returns a Set or Collection. Set offers more guarantees than Collection (even if it's just the guarantee not to include one element twice). (if I'm designing a specific class or interface, I'm being nicer to my consumers and stricter on my subclassers/implementers if I use Set.)
  3. designing a class that implements the interface Set or Collection. Similar issues as #2. Users of my class/interface get more guarantees, subclassers/implementers have more responsibility.
  4. designing an interface that extends the interface Set or Collection. Very similar to #3.
  5. writing code that uses a Set or Collection. Here I might as well use Set; the only reasons for me to use Collection is if I get back a Collection from someone else's code, or if I have to handle a collection that contains duplicates.
like image 351
Jason S Avatar asked May 04 '09 17:05

Jason S


People also ask

What is the difference between Set and collection?

Solution : A set is a well-defined collection of distinct objects. A collection is simply a group of the objects. The members of the set should be well defined. One can identify which elements are included in the set and which not.

What is the difference between collection and Set give example?

Well-defined collections are sets. Example: The collection of good teachers in a school is not a set, It is a collection. Thus, we can say that every set is a collection, but every collection is not necessarily a set.

Why use a Set instead of a List?

Sets cannot contain duplicates, and they will simply disappear. Sets use hashing to perform look ups which makes them way faster than lists in this regard.


1 Answers

Collection is also the supertype of List, Queue, Deque, and others, so it gives you more options. For example, I try to use Collection as a parameter to library methods that shouldn't explicitly depend on a certain type of collection.

Generally, you should use the right tool for the job. If you don't want duplicates, use Set (or SortedSet if you want ordering, or LinkedHashSet if you want to maintain insertion order). If you want to allow duplicates, use List, and so on.

like image 115
Michael Myers Avatar answered Sep 20 '22 14:09

Michael Myers