Question

Can anyone give confirmation that Enumeration only works with legacy collection framework classes?

I tried to retrieve elements of Vector and Stack using Enumeration, it works.

Stack<Integer> stack=new Stack<Integer>();
    stack.add(5);
    stack.add(9);
    System.out.println(stack);
    Enumeration<Integer> enumeration=stack.elements();
    while (enumeration.hasMoreElements()) {
        Integer integer = (Integer) enumeration.nextElement();
        System.out.println(integer);
    }

but when I try this on Set, it does not work. So I am in confused about whether Enumeration only works for legacy collection frameworks classes or if Enumeration works with any class.

Was it helpful?

Solution 3

Enumeartion not only work on legacy collection framework classes but also other collection framework classes. while working with Stack or Vector,we can directly enumerate like this

Stack<Integer> stack=new Stack<Integer>();
        stack.add(5);
        stack.add(9);
        System.out.println(stack);
        Enumeration<Integer> enumeration=stack.elements();
        while (enumeration.hasMoreElements()) {
            Integer integer = (Integer) enumeration.nextElement();
            System.out.println(integer);
        }
        Vector<Integer> vector=new Vector<Integer>();
        vector.add(5);
        vector.add(9);
        System.out.println(vector);
        Enumeration<Integer> enumeration2=vector.elements();
        while (enumeration2.hasMoreElements()) {
            Integer integer = (Integer) enumeration2.nextElement();
            System.out.println(integer);
        }

but when we are working with collection framework classes like HashSet,ArrayList etc we can enumerate by passing the object of the collection as argument to the Vector or Stack constructor.see the below code

Set<Integer> set=new HashSet<Integer>();
        set.add(6);
        set.add(7);
        System.out.println(set);
        Enumeration<Integer> enumeration3=new Vector(set).elements();
        while (enumeration3.hasMoreElements()) {
            Integer integer = (Integer) enumeration3.nextElement();
            System.out.println(integer);
        }
        List<Integer> list=new ArrayList<Integer>();
        list.add(12);
        list.add(23);
        System.out.println(list);
        Enumeration<Integer> enumeration4=new Vector(list).elements();
        while (enumeration4.hasMoreElements()) {
            Integer integer = (Integer) enumeration4.nextElement();
            System.out.println(integer);
        }

OTHER TIPS

More recent Java collections have their own iterators that do a similar job. You also have things like the foreach loop to make things easier.

when I try this in set then It is not working

Assuming you mean java.util.Set, that interface does not have a method that returns an Enumeration object, so it is no surprise that it will not work.

I'm also going to assume that since you are talking about legacy software, you do know about Iterator and Iterable.


Making a lot of assumptions here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top