4 ms·
More practically, a List is too expressive, if all you need is a Set - which is true and generally good advice to use Set instead of List, if possible.
by maest 5y ago
More practically, a List is too expressive, if all you need is a Set - which is true and generally good advice to use Set instead of List, if possible.
- enriquto 5y agoFrom what kind of programming hellhole do you come from, where using Set instead of List is good advice? Lists are very natural constructs for computers, sets aren't.
- RHSeeger 5y agoA set implies a uniqueness constraint that a list does not. Generally a list is ordered by contract, whereas whether or not a set is ordered depends on the implementation.
- Jtsummers 5y agoSets specify a particular interface but not a particular implementation or backing data structure. You can substitute different implementations (including ones backed by lists, vectors, hash tables, and others) depending on your particular needs.
- nkingsy 5y agoSets generally will give you constant time lookup where lists generally will not.
- dgb23 5y agoThe "if all you need" part is very important here. Without it the previous sentence makes no sense at all. In addition to that, the Set is more "expressive" than the list in the right context. Or rather it expressing things more directly.