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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:20:00+00:00 2026-06-14T14:20:00+00:00

I’m mapping x,y values onto a Cartesian plane with a HashMap. What would be

  • 0

I’m mapping x,y values onto a Cartesian plane with a HashMap. What would be an effective HashCode for very small x, very large y values?

currently I am using :

 public int hashCode() {
    return ((y * 31) ^ x);

 // & Typical x,y values would be, (with many collisions on x):
  [4, 1000001] [9, 1000000] [5, 999996] [6, 999995] [4, 999997] 
  [6, 999997] [6, 1000003] [10, 999994] [8, 999997] [10, 999997] 
  [5, 999999] [4, 999998] [5, 1000003] [2, 1000005] [3, 1000004] 
  [6, 1000000] [3, 1000005]

I am inserting both x,y pairs into the key of a hashmap with a .put method, to avoid any duplicate x,y pairs. Not sure if that is the most effective solution either.

  • 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-14T14:20:02+00:00Added an answer on June 14, 2026 at 2:20 pm

    Sometimes the best way to know is to just run some brute force tests on your ranges. Ultimately though, you can always write a hash function and go back and fix it later if your getting poor performance. Premature optimization is evil. Still, it’s easy to test hashing.

    I ran this program and got 0 collisions:

    import java.util.HashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    
    public class Testing {
    
        public static void main(String[] args) {
            int minX = 0;
            int minY = 100000;
            int maxX = 20;
            int maxY = 2000000;
    
            Map<Integer, Integer> hashToCounts = new HashMap<Integer, Integer>();
            for (int x = minX; x < maxX; x++) {
                for (int y = minY; y < maxY; y++) {
                    int hash = hash(x, y);
                    Integer count = hashToCounts.get(hash);
                    if (count == null)
                        count = 0;
                    hashToCounts.put(hash, ++count);
                }
            }
    
            int totalCollisions = 0;
            for (Entry<Integer, Integer> hashCountEntry : hashToCounts.entrySet())
                if (hashCountEntry.getValue() > 1)
                    totalCollisions += hashCountEntry.getValue() - 1;
    
            System.out.println("Total collisions: " + totalCollisions);
        }
    
        private static int hash(int x, int y) {
            return 7 + y * 31 + x * 23;
        }
    }
    

    And the output:

    Total collisions: 0

    Note that my function was 7 + y * 31 + x * 23.

    Of course, don’t take my word for it. Mess with the ranges to tweak it to your data set and try calculating it yourself.

    Using your (y * 31) ^ x gave me:

    Total collisions: 475000

    And using just x * y:

    Total collisions: 20439039

    Be warned that this program can use a pretty good chunk of memory and computing power. I ran it on a pretty powerful server. I have no idea how it’ll run on a local machine.

    Some good rules to follow for hashing are:

    • Mix up your operators. By mixing your operators, you can cause the results to vary more. Using simply x * y in this test, I had a very large number of collisions.
    • Use prime numbers for multiplication. Prime numbers have interesting binary properties that cause multiplication to be more volatile.
    • Avoid using shift operators (unless you really know what you’re doing). They insert lots of zeroes or ones into the binary of the number, decreasing volatility of other operations and potentially even shrinking your possible number of outputs.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I used javascript for loading a picture on my website depending on which small
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.