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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:57:13+00:00 2026-05-26T14:57:13+00:00

I want to be able to switch between two equals-Implementations, but I’m not sure

  • 0

I want to be able to switch between two equals-Implementations, but I’m not sure if the Equivalence class of Google Guava maybe provides this functionality. Let’s say I have two equals methods equalsContent() and equalsKeys() or something like that I somehow want to delegate the equals method to one of the two private methods (and the same for the two hashCode methods).

Well, I’m somehow not sure what the usage of the Equivalence abstract class and the Equivalences class (static methods) is.

Besides, how would you implement the desired properties described above? I could use another method which simply sets a flag or an enum to the value and implement the two equals and hash methods inside an enum with two abstract methods (equals(), hashCode()) and simply call enum.equals() or enum.hashCode() in the equals() and hashCode() method. What do you think?

  • 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-26T14:57:14+00:00Added an answer on May 26, 2026 at 2:57 pm

    I think the enum approach would make sense from an object-oriented point of view, but it’s dangerous. It could break the equals() and hashCode() contract (reflexivity, symmetry, and transitivity). For example, inserting an instance that uses the first equivalence strategy and an instance that uses the second equivalence strategy in the same Set would cause problems.

    If you want different equivalence relationships, you should keep those out of your class. Equivalence lets you do just that: you extract the equivalence logic (equals() / hashCode()) by implementing Equivalence and overriding the doHash() and doEquivalent() methods.

    Then, when you want to use a Collection based on one equivalence or the other, you use Equivalence.wrap(). For example, you could emulate an IdentityHashSet by doing:

    Set<Equivalence.Wrapper<String>> identityHashSet = Sets.newHashSet();
    
    String a1 = "a";
    String a2 = new String("a");
    String b = "b";
    
    identityHashSet.add(Equivalences.identity().wrap(a1));
    identityHashSet.add(Equivalences.identity().wrap(a2));
    identityHashSet.add(Equivalences.identity().wrap(a3));
    
    // identityHashSet contains "a", "a", and "b"
    // a standard HashSet would only contain "a" and "b"
    // while a1 and a2 are equal according to String.equals(), they are different instances.
    

    Of course, you could use ForwardingSet to automate the wrapping / unwrapping of your elements.

    There is more info in this Guava issue.

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

Sidebar

Related Questions

I have two apps that I want to be able to switch between without
i have two iphone apps and want to they able to switch via a
i need to switch between two variables so i am able to serve two
I have a matplotlib figure that I want to be able to switch between
I need to be able to switch between two third-party applications based on the
I have three Microsoft Access databases. I want to be able to switch between
I have GridView and I want to be able to switch between adapters. Context:
I want to be able to switch between a UITableView and a standard UIView
I want to be able to switch from the current db to multiple dbs
I'd like to make my iPhone app to be able to switch between skins

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.