which one do you prefer? i want to make a finite automata in java,. it's more efficient using vector or set???
A set is more efficient at insertions and deletions while a vector does these things slower. However, you cannot store elements at indices of your choice as you can in a vector.
Lookups are fastest in a vector, while they can be a tad bit slower in sets.
If your data is constant, you should go with a vector. If you will have frequent changes, you should go with a set.
A Vector is a class. A Set is an interface. I would use an ArrayList instead of a Vector anyway, if you're not doing something that needs to be threadsafe. Or a standard array if it's not going to get resized.
It really depends on your application though. Specifically, Sets don't allow duplicate elements, whereas Arrays (Arraylists, Vectors) do.
Personally I would use an array, unless it needed to have some kind of special functionality (resizing, no duplicate elements, etc.)
I'd rather use a list implementation instead of a vector. The vector is thread safe, but it carries some overhead due to that. List is not thread safe, but you can always synchronize every call to add, remove and get elements.
The set will have some odd behaviour, like casablanca said, if you are trying to get your elements in a particular order. Also, the set will act as such, and will never allow you to repeat elements in case you need to.
Both are for different kind of activities: vectors and lists are both for the same tasks, implemented different. The set will be only for such cases when you need it to function like a set.
I think that the Set is more applied to implement a Finite Automata. For instance, you should use the Set to guarantee no duplicated elements. By the Automata definition you have a set of states, a set of symbols and a set of accepted states, so a Set could fit on your requirements.
A Vector
and a Set
are two different data structures; they are not interchangeable for the same purpose.
- A
Vector
contains elements in a defined order and can contain duplicates (it's a list). - A
Set
doesn't have an inherent order and cannot contain duplicates (it's a bag, in which elements are not ordered).
Which you should use totally depends on how you design and implement your application; without knowing how you did that, it's not possible to say whether you should use a Vector
or a Set
.
Note: class java.util.Vector
is a legacy collection class, you should really use java.util.ArrayList
instead.