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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:17:40+00:00 2026-05-22T03:17:40+00:00

I have to implement a class whose instances have a bidirectional relation to each

  • 0

I have to implement a class whose instances have a bidirectional relation to each other. For example I have the class FooBar which should offer the method sameAs(FooBar x) and maintain a Set for each instances containing its equivalent instances. So if I call foo.sameAs(bar), the Set in foo should contain bar and vice versa. Invoking bar.sameAs(foo) doesn’t work, of course.

For clarifiction: the instances of this class are only semantically equal. equals should still return false.

The solutions I’ve come up with is either to implement a private method internalSameAs(FooBar x) which is invoked from sameAs(FooBar x) or to use a static method sameAs(FooBar x, FooBar y).

Solution 1:

class FooBar {
    Set<FooBar> sameAs = new HashSet<FooBar>();

    public void sameAs(FooBar x) {
        this.internalSameAs(x);
            x.internalSameAs(this);
        }

        public void internalSameAs(FooBar x) {
            sameAs.add(x);
        }
    }

Solution 2:

class FooBar {
    Set<FooBar> sameAs = new HashSet<FooBar>();

    public static void sameAs(FooBar x, FooBar y) {
        x.sameAs.add(y);
        y.sameAs.add(x);
    }
}

Which one would you prefer and why? Or is there another way I didn’t think about?

  • 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-22T03:17:41+00:00Added an answer on May 22, 2026 at 3:17 am

    are you flexible with the data structures to be used? If so you could use a Multimap (from Guava Collections) that is static amongst all the instances of the class FooBar. In that Multimap you can have the keys as FooBar references (or a unique id if you have one) and the values would be the references (or id.s) of the FooBars that have the sameAs relation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In C++, is it possible to have a base plus derived class implement a
I have been looking to implement a custom class of : IList<ArraySegment<byte>> this will
I have a bunch of class who implement a common interface : Command. And
interfaces provide a useful abstraction capability. One can have a class Foo implement some
I have a generic class that I'm trying to implement implicit type casting for.
If I have a class that needs to implement an interface but one or
I have a view controller class that has to implement several protocols. Too keep
I have a parent and child class that both need to implement IDisposable .
i have this class called MemoryManager, it is supposed to implement a simple smart
I have this question: Does implementing a custom MembershipProvider class needs you to implement

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.