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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:07:24+00:00 2026-05-23T17:07:24+00:00

Here’s my hash function for Strings public class GoodHashFunctor implements HashFunctor { @Override public

  • 0

Here’s my hash function for Strings

public class GoodHashFunctor implements HashFunctor {

    @Override
    public int hash(String item) {

        String binaryRepString = "";

        for(int i = 0; i < item.length(); i++){
            // Add the String version of the binary version of the integer  version of each character in item
            binaryRepString += Integer.toBinaryString((int)(item.charAt(i)));
        }


        long longVersion = Long.parseLong(binaryRepString, 2) % Integer.MAX_VALUE;


        return (int) longVersion;

    }

}

However, when I try hashing large Strings (around 10-15 characters), I’m getting errors because when it tries to parseLong, it dies because it’s too big a number.

What do you all think I should do? And my professor said we can’t use Java’s hashCode()

I saw a similar post where the best answer was to hash this way:

int hash=7;

for (int i=0; i < strlen; i++) {
    hash = hash*31+charAt(i);
}

But wouldn’t I run into the same problem? I guess it’d probably take a lot longer Strings to break it this new way. I dunno I’m fairly confused…

  • 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-23T17:07:25+00:00Added an answer on May 23, 2026 at 5:07 pm

    Why do you need to convert each character into a string (and that too in binary form) before converting it into a long? Why not just have a long value to which you add the char?

    This is homework, so I’m not posting code. You can also see any good algorithm book or search the web) for more about hashing.

    Edit:
    I understand you don’t want to just sum them up because anagrams will all have the same hash value. But I think you already know how to avoid that. Notice how by concatenating bits, you are basically adding bits to a value after having shifted them by some positions. i.e. “10101”+”10001″ is the same as 1010100000+10001 – 21<<5+17.

    By shifting each character by an amount proportional to its position in the string, the value added to the hash depends on both the value and position of the character. Also, observe the same effect can be had by simply multiplying rather than scaling.

    Another thing to watch out for is the fact that a long has only 64 bits. you can only pack so many chars into it before it starts to overflow. So most practical hash functions take the value modulo some number. Of course that means there is only a limited number of possible hash values for an unlimited number of input strings. Collisions are inevitable, but well chosen values for your shift/multiplier and mod can minimize the number of collisions.

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

Sidebar

Related Questions

Here's my test function (c#, visual studio 2010): [TestMethod()] public void TestGetRelevantWeeks() { List<sbyte>
Here's a problem I ran into recently. I have attributes strings of the form
Here is what I am trying to achieve in PHP: I have this string:
Here is my simplified data structure: Object1.h template <class T> class Object1 { private:
Here my code: $(document).ready(function() { $('#mid_select').live('click', function(e){ $('#middle').load( $(this).attr('href') + ' #middle'); var page
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here is a simplification of my database: Table: Property Fields: ID, Address Table: Quote
Here is my code, which takes two version identifiers in the form 1, 5,

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.