2

I know what Sets are and common operations on sets like union, intersection, difference, subset. However i don't understand in which situations are set based operations desired? Any real world examples? What are the advantages of using set vs using a list or a Hash? If i have two lists then i can find the union,intersection of those lists too. So why use Sets?

Edit I specifically want to know real world situations where i should use a set instead of a list.

newbie
  • 1,485
  • 2
  • 18
  • 43
  • why the down vote? If you down vote then please provide a comment – newbie Nov 11 '11 at 00:09
  • From the FAQ: _You should only ask practical, answerable questions based on actual problems that you face._ – Charlie Salts Nov 11 '11 at 00:14
  • So you are saying this is not a practical question? I'm working with data structures in my program and i'm wondering if i should use a List or a Hash or a Set? And i think its answerable too, so i don't understand your concerns. – newbie Nov 11 '11 at 00:15
  • 1
    partly duplicate http://stackoverflow.com/questions/1035008/what-is-the-difference-between-set-and-list – gigadot Nov 11 '11 at 00:17
  • Hash isn't really a collection. It is an algorithm that is used to index the List or Set. You can actually real this in Java API. – gigadot Nov 11 '11 at 00:18
  • @newbie: I suggest you try being more specific about what it is you're trying to accomplish. What sort of data are you working with? What operations are you wanting to do with it? Perhaps post a snippet of the code you're working with. – Charlie Salts Nov 11 '11 at 00:26

2 Answers2

4

Set guarantees there there is no duplicate object in it. List doesn't so you can have multiple entries of "equal" objects in a list. There are million of things that you can use set and it will make your life much easier, for example, a set of countries, a set of username, etc. If you use a list to store these data, you will need to check whether your list has already contained the same element or not before adding the new one unless the list is allowed to have duplicates.

In other words, set may be considered as a list without any duplicates. However, the interface of Set and List aren't really the same in Java. For example, you aren't able to get the element at certain position in a set. This is because position is not important in the set (but it is for a list). Therefore, selecting which data collection to use depends entirely on the purpose.

I, myself, found that Set is very useful in many cases and reduces the amount of checking for duplicates. One of my use cases is to use set to find how many chemical elements are in a molecule. The molecule contains a list of atom objects and each atom is associated to a element symbol so in order to find the type of element, I loop over all the atoms and add the element to an element set. All the duplicates are removed without hassle.

gigadot
  • 8,879
  • 7
  • 35
  • 51
1

Among other things, sets typically guarantee access times of O(logN). They also enforce only one entry with a given value (by throwing exceptions when you try to add a duplicate).

Hashes typically offer O(1) access, but do not guarantee uniqueness.

drdwilcox
  • 3,833
  • 17
  • 19