Wildcards and generic errors

The code below is a fling out of a bad idea to get Enumeration to work in the new foreach loop, however I would like it to compile as I continue to run into the problem with generics and wild cards. For some reason, I cannot figure out how to fix this.

So what changes do you need to make to compile?

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;


public class Main
{
    private ZipFile zipFile;

    public Set<String> entries()
    {
        final Set<String>                             names;
        final Enumeration<? extends ZipEntry>         enumeration;
        final IterableEnumeration<? extends ZipEntry> iteratable;

        names = new HashSet<String>();

        // zipFile.entries() returns "Enumeration<? extends ZipEntry>"
        enumeration = zipFile.entries();

        // this line won't compile
        iteratable  = new IterableEnumeration<? extends ZipEntry>(enumeration);

        for(final ZipEntry entry : iteratable)
        {
            if(!(entry.isDirectory()))
            {
                names.add(entry.getName());
            }
        }

        return (names);
    }
}

class IterableEnumeration<T>
    implements Iterable<T>
{
    private final Enumeration<T> enumeration;

    public IterableEnumeration(final Enumeration<T> e)
    {
        enumeration = e;
    }

    public Iterator<T> iterator()
    {
        final EnumerationIterator<T> iterator;

        // yeah cannot do this since an Iterable is supposed to work multiple times on the same object and Enumeration is descructive...
        iterator = new EnumerationIterator<T>(enumeration);

        return (iterator);
    }
}

class EnumerationIterator<T>
    implements Iterator<T>
{
    private final Enumeration<T> enumeration;

    public EnumerationIterator(final Enumeration<T> e)
    {
        enumeration = e;
    }

    public boolean hasNext()
    {
        return (enumeration.hasMoreElements());
    }

    public T next()
    {
        return (enumeration.nextElement());
    }

    public void remove()
    {
        throw new UnsupportedOperationException("Cannt remove via an Enumeration");
    }
}

Mistake:

Main.java:26: unexpected type
found   : ? extends java.util.zip.ZipEntry
required: class or interface without bounds
        iteratable  = new IterableEnumeration<? extends ZipEntry>(enumeration);
                                             ^
1 error
+3
source share
1 answer

You cannot specify a wildcard when constructing a parameterized type. This is the correct syntax:

iteratable  = new IterableEnumeration<ZipEntry>(enumeration);

, Iterable Enumeration, Enumeration , Iterable, Iterator , . for-loops, Iterable .


, . , Enumeration - "? extends ZipEntry". , :

class IterableEnumeration<T>
  implements Iterable<T>
{

  private final Enumeration<? extends T> enumeration;

  public IterableEnumeration(final Enumeration<? extends T> e)
  {
    enumeration = e;
  }

  ...

class EnumerationIterator<T>
  implements Iterator<T>
{

  private final Enumeration<? extends T> enumeration;

  public EnumerationIterator(final Enumeration<? extends T> e)
  {
    enumeration = e;
  }

  ...

: "IterableEnumeration<T> Enumeration T."

+5

Source: https://habr.com/ru/post/1704370/


All Articles