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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:44:44+00:00 2026-06-08T23:44:44+00:00

I need a specialised hash function h(X,Y) in Java with the following properties. X

  • 0

I need a specialised hash function h(X,Y) in Java with the following properties.

  1. X and Y are strings.
  2. h(X,Y) = h(Y,X).
  3. X and Y are arbitrary length strings and there is no length limit on the result of h(X,Y) either.
  4. h(X,Y) and h(Y,X) should not collide with h(A,B) = h(B,A) if X is not equal to A and Y is not equal to B.
  5. h() does not need to be a secure hash function unless it is necessary to meet the aforementioned requirements.
  6. Fairly high-performant but this is an open-ended criterion.

In my mind, I see requirements 2 and 4 slightly contradictory but perhaps I am worrying too much.

At the moment, what I am doing in Java is the following:

public static BigInteger hashStringConcatenation(String str1, String str2) {
    BigInteger bA = BigInteger.ZERO;
    BigInteger bB = BigInteger.ZERO;
    for(int i=0; i<str1.length(); i++) {
        bA = bA.add(BigInteger.valueOf(127L).pow(i+1).multiply(BigInteger.valueOf(str1.codePointAt(i))));
    }
    for(int i=0; i<str2.length(); i++) {
        bB = bB.add(BigInteger.valueOf(127L).pow(i+1).multiply(BigInteger.valueOf(str2.codePointAt(i))));
    }
    return bA.multiply(bB);
}

I think this is hideous but that’s why I am looking for nicer solutions. Thanks.

Forgot to mention that on a 2.53GHz dual core Macbook Pro with 8GB RAM and Java 1.6 on OS X 10.7, the hash function takes about 270 micro-seconds for two 8 (ASCII) character Strings. I suspect this would be higher with the increase in the String size, or if Unicode characters are used.

  • 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-08T23:44:45+00:00Added an answer on June 8, 2026 at 11:44 pm

    Today I’ve decided to add my solution for this hash function problem. It was not tested very good and I did not measure its performance, so you can feed me back with your comments. My solution is situated below:

    public abstract class HashUtil {
        //determines that we want hash, that has size of 32 integers ( or 32*32 bits )
        private static final int hash_size = 32;
    
        //some constants that can be changed in sake of avoiding collisions
        private static final BigInteger INITIAL_HASH = BigInteger.valueOf(7);
        private static final BigInteger HASH_MULTIPLIER = BigInteger.valueOf(31);
        private static final BigInteger HASH_DIVIDER = BigInteger.valueOf(2).pow(32*hash_size);
    
        public static BigInteger computeHash(String arg){
            BigInteger hash = new BigInteger(INITIAL_HASH.toByteArray());
            for (int i=0;i<arg.length()/hash_size+1;i++){
                int[] tmp = new int[hash_size];
                for(int j=0;j<Math.min(arg.length()-32*i,32);j++){
                    tmp[i]=arg.codePointAt(i*hash_size+j);
                }
                hash = hash.multiply(HASH_MULTIPLIER).add(new BigInteger(convert(tmp)).abs()).mod(HASH_DIVIDER);
            }
            //to reduce result space to something meaningful
            return hash;
        }
    
        public static BigInteger computeHash(String arg1,String arg2){
            //here I don't forgot about reducing of result space
            return computeHash(arg1).add(computeHash(arg2)).mod(HASH_DIVIDER);
        }
    
        private static byte[] convert(int[] arg){
            ByteBuffer byteBuffer = ByteBuffer.allocate(arg.length*4);
            IntBuffer intBuffer = byteBuffer.asIntBuffer();
            intBuffer.put(arg);
            return byteBuffer.array();
        }
    
        public static void main(String[] args){
            String firstString="dslkjfaklsjdkfajsldfjaldsjflaksjdfklajsdlfjaslfj",secondString="unejrng43hti9uhg9rhe3gh9rugh3u94htfeiuwho894rhgfu";
            System.out.println(computeHash(firstString,secondString).equals(computeHash(secondString,firstString)));
        }
    

    }

    I suppose that my solution should not produce any collision for single string with length less then 32 (to be more precise, for single string with length less then hash_size variable value). Also it is not very easy to find collisions (as I think). To regulate hash conflicts probability for your particular task you can try another prime numbers instead of 7 and 31 in INITIAL_HASH and HASH_MULTIPLIER variables. What do you think about it? Is it good enought for you?

    P.S. I think that it will be much better if you’ll try much bigger prime numbers.

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

Sidebar

Related Questions

I need to specialize the hash function for unordered_map so I can use int
Background We need to develop a specialised CMS (internal use only) to support a
I have a sample class where I need to specialize the Print function if
Need a map reduce function by mongo in php This my mongo structure [_id]
Need help, function getFamily() { FB.api('/me/family', function(response) { alert(JSON.stringify(response)); }); } With the above
I need a sensible way to draw arbitrary text files into a C# program,
I have a need for a fairly specialised collection .NET, and I don't think
I need to create a specific shape for the Tab. It should look like
I need to store data related to items where there will be various different
Is it possible to call a function defined in a non-specialised template class from

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.