java - when to use Set vs. Collection? -


Is there any practical difference between set and archive in Java ? Apart from the fact that a archive can include the same element twice? They have only one way.

(For example, set to accept set s but to use the libraries accepting the collection Give more options s?)

Edit: I can think of at least 5 different situations to judge this question . Can anyone come forward with someone else? I want to make sure that I understand subtlety here.

  1. Design a method that accepts a log of set or archive . archive is more common and accepts more possibilities of input. (If I am preparing a specific category or interface, then I am doing good for my consumers and strict on my subclassers / implementers if I use archive .) < / Li>
  2. returns set or archive . Provides more guarantees than set archive (even if it is guaranteed not to include only one element twice). (If I am preparing a specific class or interface, then I am doing good for my consumers and strict on my sub-clasers / implementers if I use set .) < / Li>
  3. implements the interface set or archive . Problems like # 2
  4. Increases the interface set or archive Users of my class / interface receive more guarantees to design an interface . Like the # 3
  5. The writing code that uses set or archive Here I can also use the set ; The only reason for using archive for me is that if I return archive with some other code, or if I have to handle the archive in which there are duplicates.

also list , Q , deck , and others, so it gives you more options. For example, I use archive as a parameter for library methods , Which should not be clearly dependent on a particular type of collection.

Normally, you should use the right tools to work. If you do not want to duplicate, use set (or SortedSet if you want to order, or use LinkedHashSet You want to keep the insertion order). If you want to allow duplicates, use list , and so on.


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -