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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:42:52+00:00 2026-06-13T02:42:52+00:00

An existing system written in Java uses the hashcode of a string as its

  • 0

An existing system written in Java uses the hashcode of a string as its routing strategy for load balancing.

Now, I cannot modify the system but need to generate strings that share the same hashcode to test the worst condition.

I provide those strings from commandline and hope the system will route all these strings into the same destination.

Is it possible to generate a large numbers of strings that share the same hashcode?

To make this question clear:

String[] getStringsInSameHashCode(int number){
    //return an array in length "number"
    //Every element of the array share the same hashcode. 
    //The element should be different from each other
}

Remarks: Any hashCode value is acceptable. There is no constraint on what the string is. But they should be different from each other.

EDIT:
Override method of String class is not acceptable because I feed those string from command line.

Instrumentation is also not acceptable because that will make some impacts on the system.

  • 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-13T02:42:54+00:00Added an answer on June 13, 2026 at 2:42 am

    see a test method, basically, so long as you match,
    a1*31+b1 = a2*31 +b2, which means (a1-a2)*31=b2-b1

    public void testHash()
    {
        System.out.println("A:" + ((int)'A'));
        System.out.println("B:" + ((int)'B'));
        System.out.println("a:" + ((int)'a'));
    
        System.out.println(hash("Aa".hashCode()));
        System.out.println(hash("BB".hashCode()));
        System.out.println(hash("Aa".hashCode()));
        System.out.println(hash("BB".hashCode()));
    
    
        System.out.println(hash("AaAa".hashCode()));
        System.out.println(hash("BBBB".hashCode()));
        System.out.println(hash("AaBB".hashCode()));
        System.out.println(hash("BBAa".hashCode()));
    
    }
    

    you will get

    A:65
    B:66
    a:97
    2260
    2260
    2260
    2260
    2019172
    2019172
    2019172
    2019172
    

    edit: someone said this is not straightforward enough. I added below part

        @Test
        public void testN() throws Exception {
            List<String> l = HashCUtil.generateN(3);
            for(int i = 0; i < l.size(); ++i){
                System.out.println(l.get(i) + "---" + l.get(i).hashCode());
            }
        }
    AaAaAa---1952508096
    AaAaBB---1952508096
    AaBBAa---1952508096
    AaBBBB---1952508096
    BBAaAa---1952508096
    BBAaBB---1952508096
    BBBBAa---1952508096
    BBBBBB---1952508096
    

    below is the source code, it might be not efficient, but it work:

    public class HashCUtil {
    
        private static String[] base = new String[] {"Aa", "BB"};
    
        public static List<String> generateN(int n)
        {
            if(n <= 0)
            {
                return null;
            }
    
            List<String> list = generateOne(null);
            for(int i = 1; i < n; ++i)
            {
                list = generateOne(list);
            }
    
            return list;
        }
    
    
        public static List<String> generateOne(List<String> strList)
        {   
            if((null == strList) || (0 == strList.size()))
            {
                strList = new ArrayList<String>();
                for(int i = 0; i < base.length; ++i)
                {
                    strList.add(base[i]);
                }
    
                return strList;
            }
    
            List<String> result = new ArrayList<String>();
    
            for(int i = 0; i < base.length; ++i)
            {
                for(String str: strList)
                {   
                    result.add(base[i]  + str);
                }
            }
    
            return result;      
        }
    }
    

    look at String.hashCode()

       public int hashCode() {
        int h = hash;
        if (h == 0) {
            int off = offset;
            char val[] = value;
            int len = count;
    
                for (int i = 0; i < len; i++) {
                    h = 31*h + val[off++];
                }
                hash = h;
            }
            return h;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an existing system written using .NET 2.0 remoting to integrate
I have an existing system built in Java we currently expand that by adding
Let's say I have an existing application written in Java which I wish to
We have a large existing website (written in Java/Spring/Hibernate/JSP) and want to add a
I'm currently trying to improve the testability of a legacy system, written in Java.
I'm working on exporting data from an existing accounting system written in C# in
We've an existing system which connects to the the back end via http (apache/ssl)
I'm interested in using an object relational mapper for an existing system which is
I'm attempting to bring sanity to an existing system by adding version control. Trouble
I'm new to stored procedure world. We have existing system with existing stored procedure,

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.