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 7498965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:39:17+00:00 2026-05-29T19:39:17+00:00

For now, I have only 3 way to compare two iterators (with String type)

  • 0

For now, I have only 3 way to compare two iterators (with String type) in scala:

// 1.
it1 sameElements it2

// 2.
it2.toList == it2.toList

// 3. 
def compare(it1:Iterator[String], it2:Iterator[String]) {
  while (it1.hasNext) {
    if (!it2.hasNext) return false
    if (it1.next != it2.next) {
      return false
    }
  }
  return !it2.hasNext
}

Is there any other good ways to do this?

  • 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-05-29T19:39:18+00:00Added an answer on May 29, 2026 at 7:39 pm

    I would use zip:

    def compare(it1:Iterator[String], it2:Iterator[String]) = {
      it1.zip(it2).forall(x => x._1 == x._2) && 
      (it1.length == it2.length)    
    }
    

    Or you can also use tail recursion:

    def compare(it1:Iterator[String], it2:Iterator[String]) : Boolean = {
      (it1 hasNext, it2 hasNext) match{
        case (true, true) => (it1.next == it2.next) && compare(it1, it2)
        case (false, false) => true
        case _ => false
      }
    }
    

    The function used for sameElements, which I recommend as it is used in the API, I modified the source signature for readability

    def compare(it1:Iterator[String], it2:Iterator[String]) = {
      while (it1.hasNext && it2.hasNext)
        if (it1.next != it2.next) return false
      !it1.hasNext && !it2.hasNext
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string which contains only numbers. Now I want to remove all
I now have a running Java program which only lacks of the final step,that
Now that I have a read-only application working, I am working on the insert
I have a wordpress application thats currently the only application on the domain. Now
I have a pretty complicated index.php now, and I would like to only run
We have a website; which, till now had only HTML pages. Now we are
I have been programming in Perl, off and on, for years now, although only
in /usr/local i now have two mysql folders: mysql-5.1.34-osx10.5-x86 mysql-5.1.37-osx10.5-x86_64 is it possible to
I have to compare two Integer objects (not int ). What is the canonical
C# I'm trying to compare two different objects (I'm only comparing identical subfields). But

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.