Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8184131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:23:42+00:00 2026-06-07T01:23:42+00:00

What is the difference between Iterator and Iterable in scala? I thought that Iterable

  • 0

What is the difference between Iterator and Iterable in scala?

I thought that Iterable represents a set that I can iterate through, and Iterator is a “pointer” to one of the items in the iterable set.

However, Iterator has functions like forEach, map, foldLeft. It can be converted to Iterable via toIterable. And, for example, scala.io.Source.getLines returns Iterator, not Iterable.

But I cannot do groupBy on Iterator and I can do it on Iterable.

So, what’s the relation between those two, Iterator and Iterable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:23:45+00:00Added an answer on June 7, 2026 at 1:23 am

    In short: An Iterator does have state, whereas an Iterable does not.

    See the API docs for both.

    Iterable:

    A base trait for iterable collections.

    This is a base trait for all Scala collections that define an iterator
    method to step through one-by-one the collection’s elements.
    […] This trait implements Iterable’s foreach method by stepping
    through all elements using iterator.

    Iterator:

    Iterators are data structures that allow to iterate over a sequence of
    elements. They have a hasNext method for checking if there is a next
    element available, and a next method which returns the next element
    and discards it from the iterator.

    An iterator is mutable: most operations on it change its state. While
    it is often used to iterate through the elements of a collection, it
    can also be used without being backed by any collection (see
    constructors on the companion object).

    With an Iterator you can stop an iteration and continue it later if you want. If you try to do this with an Iterable it will begin from the head again:

    scala> val iterable: Iterable[Int] = 1 to 4
    iterable: Iterable[Int] = Range(1, 2, 3, 4)
    
    scala> iterable.take(2)
    res8: Iterable[Int] = Range(1, 2)
    
    scala> iterable.take(2)
    res9: Iterable[Int] = Range(1, 2)
    
    scala> val iterator = iterable.iterator
    iterator: Iterator[Int] = non-empty iterator
    
    scala> if (iterator.hasNext) iterator.next
    res23: AnyVal = 1
    
    scala> if (iterator.hasNext) iterator.next
    res24: AnyVal = 2
    
    scala> if (iterator.hasNext) iterator.next
    res25: AnyVal = 3
    
    scala> if (iterator.hasNext) iterator.next
    res26: AnyVal = 4
    
    scala> if (iterator.hasNext) iterator.next
    res27: AnyVal = ()
    

    Note, that I didn’t use take on Iterator. The reason for this is that it is tricky to use. hasNext and next are the only two methods that are guaranteed to work as expected on Iterator. See the Scaladoc again:

    It is of particular importance to note that, unless stated otherwise,
    one should never use an iterator after calling a method on it. The two
    most important exceptions are also the sole abstract methods: next and
    hasNext.

    Both these methods can be called any number of times without having to
    discard the iterator. Note that even hasNext may cause mutation —
    such as when iterating from an input stream, where it will block until
    the stream is closed or some input becomes available.

    Consider this example for safe and unsafe use:

    def f[A](it: Iterator[A]) = {
      if (it.hasNext) {            // Safe to reuse "it" after "hasNext"
        it.next                    // Safe to reuse "it" after "next"
        val remainder = it.drop(2) // it is *not* safe to use "it" again after this line!
        remainder.take(2)          // it is *not* safe to use "remainder" after this line!
      } else it
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The difference between Chr and Char when used in converting types is that one
Can any one explain difference between position and anchor point in cocos-2D with some
What is the performance difference between using an iterator to loop through an STL
Possible Duplicate: difference between Iterator and Listiterator? Recently,while I was goint through javadocs, I
Difference between start-pointers and interior-pointers and in what situation we should prefer one over
When implementing iterator using yield return , is there any difference between returning IEnumerator
I've been doing some performance testing, mainly so I can understand the difference between
As per my knowledge the main difference between Iterator and ListIterator is Iterator :
What is the difference between an Iterator and a Generator?
Just starting to learn boost::filesystem. What is a difference between directory_iterator and basic_path::iterator? Do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.