Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Enumeration to Iterator

I have the following implicit conversion for java.util.Enumerations

   implicit def enumerationIterator[A](e : Enumeration[A]) : Iterator[A] = {
     new Iterator[A] {
        def hasNext = e.hasMoreElements
        def next = e.nextElement
        def remove = throw new UnsupportedOperationException()
     }
   }

Unfortunately it does not work for ZipFile.entries because it returns an Enumeration<? extends ZipEntry> (see related question) and Scalac keeps telling me

type mismatch; found : java.util.Iterator[?0] 
   where type ?0 <: java.util.zip.ZipEntry 
   required: Iterator[?]

I can't figure out how to make the conversation work in sth. like

List.fromIterator(new ZipFile(z).entries))
like image 884
Peter Kofler Avatar asked Jun 28 '09 09:06

Peter Kofler


People also ask

Is Enum an Iterable?

Enumeration is like an Iterator , not an Iterable . A Collection is Iterable . An Iterator is not. There is no Enumerable equivalent to Iterable .

Which is faster iterator or enumeration?

In the example of the micro-benchmark given, the reason the Enumeration is faster than Iterator is because it is tested first. ;) The longer answer is that the HotSpot compiler optimises a whole method when a loop has iterated 10,000 times.

How do you enumerate a list in Java?

Enumeration[] array = Enumeration. values(); List<Enumeration> list = Arrays. asList(array);


2 Answers

List.fromIterator expects a scala.Iterator but your implicit is returning a java.util.Iterator.

This works

import java.util.Enumeration

implicit def enum2Iterator[A](e : Enumeration[A]) = new Iterator[A] {
  def next = e.nextElement
  def hasNext = e.hasMoreElements
}

import java.util.zip.{ZipFile, ZipEntry}
val l = List.fromIterator(new ZipFile(null:java.io.File).entries)

Adding one import at the top prevents compilation

import java.util.Iterator

There's been some discussion about unifying Scala and Java in 2.8 by just using java.util.Iterator. On the downside, Java's Iterator has a remove method which makes no sense for Scala's immutable collections. UnsupportedOperationException? Blech! On the plus side that makes stuff like this error go away.

Edit: I've added a Trac issue that the error message would have been clearer had it said "required: scala.Iterator[?]" https://lampsvn.epfl.ch/trac/scala/ticket/2102

like image 189
James Iry Avatar answered Oct 02 '22 11:10

James Iry


As far as I know, Enumeration in Scala 2.7.x has an "elements" method and Scala 2.8.0 has an "iterator" method returing an Iterator. Why not use them?

Oh, never mind, never mind. Java's enumeration.

like image 39
Daniel C. Sobral Avatar answered Oct 02 '22 12:10

Daniel C. Sobral