0

See the two different implementations that return Enumeration<?>

Vector<String> v = new Vector<>(map.keySet());
return v.elements();

List<String> v = new ArrayList<>(map.keySet());
return Collections.enumeration(v);

Please note that the return type here Enumeration<?>, cannot be changed.

Is there any advantage of using one over the other?

As Enumeration classes are synchronized, should I better stick with Vector?


My intention was not to compare Vector vs ArrayList, but to find the better implementation from the above two specific code usages. The answers here are also different from What are the differences between ArrayList and Vector?

So I don't think this qualifies as a duplicate.

Gayan Weerakutti
  • 11,904
  • 2
  • 71
  • 68
  • 3
    Possible duplicate of [What are the differences between ArrayList and Vector?](https://stackoverflow.com/questions/2986296/what-are-the-differences-between-arraylist-and-vector) – Kent Shikama Dec 23 '17 at 07:39
  • 1
    Vector and Enumeration are obsolete since Java 2. Just don't use them anymore. – JB Nizet Dec 23 '17 at 07:40
  • I don't think this is a duplicate as it is more specific than just ArrayList and Vector. – Gayan Weerakutti Dec 23 '17 at 07:41
  • @JBNizet Enumeration> is already being used. so it is not possible to change. I just want to know what is the better implementation from the two. – Gayan Weerakutti Dec 23 '17 at 07:47
  • 2
    From my comment, I think you can safely conclude that Vector is obsolete since Java 2 and that you thus shouldn't use it anymore. – JB Nizet Dec 23 '17 at 07:54
  • People... still use `Enumeration`? That was also phased out in favor of `Iterator` in Java 2. – Powerlord Dec 23 '17 at 07:58
  • @Powerlord It is for an API. so not quite possible to change to `Iterable` – Gayan Weerakutti Dec 23 '17 at 08:04
  • @EJP I never said it was phased out. I said it was obsolete, and that new code shouldn't use it anymore. – JB Nizet Dec 23 '17 at 09:48
  • @Powerlord It was not 'phased out'. It is still present and still supported, and that is never going to change. I have code out there that was written with Enumeration in 1997 that is still running. – user207421 Dec 23 '17 at 17:08
  • @EJP I probably should have used the term "deprecated" instead. – Powerlord Dec 23 '17 at 23:16

4 Answers4

2

Neither. You don't need either of these. Collections.enumeration() takes a Collection argument, and Set is a Collection, and Map.keySet() returns a Set.

You just need:

return Collections.enumeration(map.keySet());
user207421
  • 305,947
  • 44
  • 307
  • 483
0

The use of these obsolete -though not deprecated- classes is often a business decision. There's not much you can do about it, except, if possible, use a different API.

I wouldn't use Vector. If you don't need the synchronisation in your own code, use the Collections Framework. If you do need the synchronisation, use the synchronisation, use the classes from java.util.concurrent.

Either way, use Collections.enumeration to convert them to an enumeration.

SeverityOne
  • 2,476
  • 12
  • 25
0

well depending on on your use case, use ArrayList if you don't care about thread safety so you don't get the additional overhead of synchronized methods of Vector, otherwise use Vector .

mehdi maick
  • 325
  • 3
  • 7
0

I think that if you want to just get Enumeration there is no difference.

Vector constoructor is as below.

public Vector(Collection<? extends E> c) {
    elementData = c.toArray();
    elementCount = elementData.length;
    // c.toArray might (incorrectly) not return Object[] (see 6260652)
    if (elementData.getClass() != Object[].class)
        elementData = Arrays.copyOf(elementData, elementCount, Object[].class);
}

ArrayList constructor is as below.

 public ArrayList(Collection<? extends E> c) {
    elementData = c.toArray();
    if ((size = elementData.length) != 0) {
        // c.toArray might (incorrectly) not return Object[] (see 6260652)
        if (elementData.getClass() != Object[].class)
            elementData = Arrays.copyOf(elementData, size, Object[].class);
    } else {
        // replace with empty array.
        this.elementData = EMPTY_ELEMENTDATA;
    }
}
fanfanta
  • 181
  • 14