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

  • Home
  • SEARCH
  • 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 6983407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:24:54+00:00 2026-05-27T18:24:54+00:00

Possible Duplicate: How to most elegantly iterate through parallel collections? Is there an accepted

  • 0

Possible Duplicate:
How to most elegantly iterate through parallel collections?
Is there an accepted Java equivalent to Python's zip()?

I have to iterate simultaneously over two Collections of objects to see if their members are equal. Is there a way of using a counter to access the object or do something similar to the for loop below ?

Boolean compareCollection(Collection<Object> c1, Collection<Object> c2)
{
     //something like -
     for (Object ob1 : c1, Object ob2 : c2){
           //check if ob1.val == ob2.val
     }
     return true;
}
  • 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-27T18:24:55+00:00Added an answer on May 27, 2026 at 6:24 pm

    Something like this:

    public static boolean compareCollection(Collection<Object> c1, Collection<Object> c2)   {
        if (c1 == null)
            return c2 == null;
        else if (c2 == null || c1.size() != c2.size())
            return false;
        Iterator<Object> it1 = c1.iterator();
        Iterator<Object> it2 = c2.iterator();
        while (it1.hasNext()) {
            Object o1 = it1.next();
            Object o2 = it2.next();
            if (!o1.equals(o2))
                return false;
        }
         return true;
    }
    

    Of course, you’ll have to parameterize the collections with the right type (which I’m guessing is not just Object) and making the appropriate comparison:

    if (!o1.val.equals(o2.val))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Best way to iterate through a directory in java? I want to
Possible Duplicate: Most efficent way of joining strings I have a List. What would
Possible Duplicate: Tools for static type checking in Python For most part I love
Possible Duplicate: What are the most common naming conventions in C? Is there a
Possible Duplicate: Delete all but the most recent X files in bash I have
Possible Duplicate: Most efficient implementation of a large number class Suppose I needed to
Possible Duplicate: What is the most efficient way to clone a JavaScript object? I
Possible Duplicate: Why use document.write? Considering the negative effects of document.write(), why are most
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: How do you send email from a Java app using Gmail? How

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.