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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:42:02+00:00 2026-06-18T01:42:02+00:00

I have a class and different equivalence rules on it (different implementation of equals

  • 0

I have a class and different equivalence rules on it (different implementation of equals and hashCode). The data is generated in one process first, where one equivalence rule is applied, and then fed to the second process, where the other equivalence rule is applied. Particularly, I am doing a lot of map operations and equals and hashCode are called implicitly by the standard library (which I do not have control on). What do you think is the best way to achieve this? I have two solutions now:

  1. Define two subclasses with different equals and hashCode. After process 1, do the conversion by initiating objects of the other subclass.
  2. Introduce mutable states in the class to indicate which equivalence rule to apply.

So which one do you think is better or is there any other good solutions?

  • 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-06-18T01:42:03+00:00Added an answer on June 18, 2026 at 1:42 am

    Finally I find that writing my own customized Map is the way to go (at least in my problem). After I dig into the scala standard library for a while, I figure out that it is extremely easy. No matter whether mutable or not, the element equality and hashCode methods in HashMap are inherited from HashTable and HashTable.Utils and are protected, meaning any subclass can override it easily. So the following is what I end up with:

    trait Equility[T] {
      def equal(t1: T, t2: T): Boolean
      def hash(t: T): Int
    }
    
    class MapWithEquility[K, V](e: Equility[K]) extends scala.collection.mutable.HashMap[K, V] {
      override def elemHashCode(key: K) = e.hash(key)
      override def elemEquals(key1: K, key2: K) = e.equal(key1, key2)
    }
    

    I did a simple test and it worked well.

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

Sidebar

Related Questions

I have two different projects and in one, I have a class that defines
I have one parent class, which is abstract class for now, for four different
Hi i have a class which contains different methods for checking database values. Each
I have class that represents users. Users are divided into two groups with different
I have a large class, which I have divided into several different class extension
I have this class that has many class members, and a lot of different
I have a Test Class, that test the access of all page with different
I have a few different classes which origin is a another class. I have
I have 2(or more) tables with different ids and class names within each tables
I have an action inside my controller class and I want two different routes

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.