Unable to iterate Java List in Scala

Sri picture Sri · Mar 14, 2011 · Viewed 12.8k times · Source

I'm using the Java Twitter4J library in a Scala project.

I'm calling the method

twitter.getFriendsStatuses()

This method returns a list of twitter4j.User objects containing statuses.

I try to iterate over them and it goes in an infinite loop over the first element:

val users:List[User] = twitter.getFriendsStatuses(userId, paging.getSinceId())
while( users.iterator.hasNext() ) {
  println(users.iterator.next().getStatus())
}

Any ideas?

Answer

axtavt picture axtavt · Mar 14, 2011

I guess users.iterator produces the new iterator each time it's evaluated. Try this:

val it = users.iterator
while(it.hasNext() ) {
   println(it.next().getStatus())
}