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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:22:08+00:00 2026-05-25T11:22:08+00:00

How would one implement equals in a java-class which implements an interface that extends

  • 0

How would one implement equals in a java-class which implements an interface that extends Iterable?

The interface

public interface MyInterface extends Iterable<String> {
...
}

The concrete class

public class MyClass implements MyInterface {

  private Set<String> myStrings = new HashSet<String>();

  @Override
  public Iterator<String> iterator() {
    return myStrings.iterator();
  }

  @Override
  public boolean equals(Object otherObject) {

How should I check that both this instance and the other instances contains the same set of strings? The easy way would to only check equals against this implementation and not the interface, but that sounds like cheating.

    if (otherObject instanceof MyClass) { ... } // easy, just myStrings.equals(...)

but

    if (otherObject instanceof MyInterface) { ... } // compare two iterators?

Or am I missing something? I must implement hashCode aswell, and if two objects are equal should not their hashcodes be identical hence equals must only check against MyClass to fullfil this contract?!

  }

}
  • 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-25T11:22:08+00:00Added an answer on May 25, 2026 at 11:22 am

    One way would be to use Guava Iterables.elementsEqual method.

    http://docs.guava-libraries.googlecode.com/git-history/release09/javadoc/com/google/common/collect/Iterables.html#elementsEqual(java.lang.Iterable, java.lang.Iterable)

    /**
     * Returns true if all elements in <code>searchFor</code> exist in
     * <code>searchIn</code>, otherwise returns false.
     * 
     * @param searchIn
     *            the collection in which to search for each element in
     *            <code>searchFor</code>
     * @param searchFor
     *            the collection of element to search for
     */
    public static boolean containsAll(@Nonnull Iterable<?> searchIn, @Nonnull Iterable<?> searchFor) {
        for (Object o : searchFor) {
            if (!Iterables.contains(searchIn, o)) {
                return false;
            }
        }
        return true;
    }
    
    /**
     * Returns true if all elements in <code>searchFor</code> exist in
     * <code>searchIn</code> and no other elements exist in
     * <code>searchIn</code>, otherwise returns false.
     * 
     * @param searchIn
     *            the collection in which to search for each element in
     *            <code>searchFor</code>
     * @param searchFor
     *            the collection of element to search for
     */
    public static boolean containsAllAndOnly(@Nonnull Iterable<?> searchIn,
            @Nonnull Iterable<?> searchFor) {
        if (Iterables.size(searchIn) != Iterables.size(searchFor)) {
            return false;
        }
    
        return containsAll(searchIn, searchFor);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; public class Main implements Runnable { private final CountDownLatch cdl1
how would one implement a back-button as a commandbutton that works universally? with back
One more question that relates to this interface . Let's say that I would
How would one implement in R the function apply.func(func, arg.list) , which takes an
How would one typically implement a service layer in an MVC architecture? Is it
I would like to implement an distributed Point-Of-Sale system, somewhat like the one described
Similar to the one here on StackOverFlow, I would be needing to implement a
How would one create a Singleton class using PHP5 classes?
How would one structure a table for an entity that can have a one
I need to implement 3 public methods that all rely on the same initial

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.