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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:53:13+00:00 2026-06-02T15:53:13+00:00

I have hashmap that has keys like this:’2+4+5′ , ‘653+65+1324+75’.(integer values delimited by a

  • 0

I have hashmap that has keys like this:’2+4+5′ , ‘653+65+1324+75’.(integer values delimited by a + sign)

What could be a good hashcode and equals method so that keys like ‘2+4+5’, ‘5+4+2’, ‘4+5+2’ … (all the permutation of 2,4,5) should return the same hashcode value and equals should return true.

I am planning to take the integer values in the key, sort them, put them in an ascending order into a string and call that strings hashcode and equals method. Suppose if I have ‘5+2+4’ then I would change that to “245” and call the strings hashcode and equals method. But this is going to be an expensive operation since each time I have to do sorting. And all the methods in the hashmap like put,get… would again be expensive

Are there any other ways of doing this in a log or linear time…

  • 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-02T15:53:16+00:00Added an answer on June 2, 2026 at 3:53 pm
    class Combination {
        final int[] parts;
    
        Combination(String str) {
            String[] strings = str.split("\\+");
            parts = new int[strings.length];
            for (int i = 0; i < parts.length; i++) {
                parts[i] = Integer.parseInt(strings[i]);
            }
            Arrays.sort(parts);
        }
    
        @Override public int hashCode() {
            return Arrays.hashCode(parts);
        }
    
        @Override public boolean equals(Object o) {
            return o instanceof Combination && Arrays.equals(parts, ((Combination) o).parts);
        }
    }
    

    Test code:

    public static void main(String[] args) {
        Set<Combination> set = new HashSet<>();
        set.add(new Combination("1+2+3"));
        set.add(new Combination("1+2+4"));
        System.out.println(set.contains(new Combination("3+2+1"))); // prints "true"
        System.out.println(set.contains(new Combination("4+2+1"))); // prints "true"
        System.out.println(set.contains(new Combination("4+3+1"))); // prints "false"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have hashmap and its keys are like folder/1.txt,folder/2.txt,folder/3.txt and value has these text
I have a List of HashMap 's which has key of type Integer and
I have come across a strange behavior of Java that seems like a bug.
I have an object A that has as an instance variable a collection of
I have a class that has a big method that calls on a lot
I'm making an Android application that has to read some values from a JSON
I have a domain object that has a Map: private Map<AutoHandlingSlotKey, LinkedHashSet<AutoFunction>> autoHandling; When
I have a HashMap as below (assuming it has 10,0000 elements) HashMap<String,String> hm =
I'm developing a Java application that has performance at its core. I have a
I have a base abstract logger class, that has an instance variable that I

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.