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.
- Design a method that accepts a log of
setorarchive.archiveis 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 usearchive.) < / Li> - returns
setorarchive. Provides more guarantees thansetarchive(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 useset.) < / Li> - implements the interface
setorarchive. Problems like # 2 - Increases the interface
setorarchiveUsers of my class / interface receive more guarantees to design an interface . Like the # 3 - The writing code that uses
setorarchiveHere I can also use theset; The only reason for usingarchivefor me is that if I returnarchivewith 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
Post a Comment