Unable to iterate Java List in Scala

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

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())
}

Leave a Reply

Your email address will not be published. Required fields are marked *