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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:09:55+00:00 2026-06-15T06:09:55+00:00

In my program, I’d like to use Map with two keys (Integers). My first

  • 0

In my program, I’d like to use Map with two keys (Integers). My first idea was to join the integers into a string in some way, eg:

String key = k1.toString()+"-"+k2.toString();

This solution didn’t look good for me: 1) ugly; 2) slow (handling numbers as a text).

I discovered other approaches here on stackoverflow. They were based on encapsulating the integers in one class – one purpose class (MyKey), or more generic one (Pair).

I tried to run some speed tests and my dummy solutions seem to be fastest. After the first shot, I tried to encapsulate the transformation integers-string in a new class (MyString) and run test also against this solution.

The maps definitions were:

Map<Pair<Integer,Integer>,String> map1 = new HashMap<>();
Map<MyKey,String> map2 = new HashMap<>();
Map<String,String> map3 = new HashMap<>();
Map<MyString,String> map4 = new HashMap<>();

The test results were (ran multiple times, seems stable):

  map: put+get=total
  1: 52+154=206
  2: 29+77=106
  3: 23+49=72
  3: 17+55=72

The solutions with string are faster. Direct concatenating of string keys is faster when searching, slower when inputting.

My questions are:

1) Why are the solutions with String faster? (one call of hashCode()?)

2) Is there any reason why shouldn’t be solutions with String used?


Additional information:

Number of records in the Map was about 6000.

Test tried to get also values for many unexisting keys. Could it change test results?

In my program, I generate permutations of boolean[N] where M values are true. Once, I get result for certain N,M; I’d like to store them for case I need them again.

And here is complete code of classes used in my example:

  class Pair<L,R> {

    private final L left;
    private final R right;

    public Pair(L left, R right) {
      this.left = left;
      this.right = right;
    }

    public L getLeft() { return left; }
    public R getRight() { return right; }

    @Override
    public int hashCode() { return left.hashCode() ^ right.hashCode(); }

    @Override
    public boolean equals(Object o) {
      if (o == null) return false;
      if (!(o instanceof Pair)) return false;
      Pair pairo = (Pair) o;
      return this.left.equals(pairo.getLeft()) &&
             this.right.equals(pairo.getRight());
    }
  }

  class MyKey {
      public Integer k1;
      public Integer k2;

      public MyKey(Integer k1, Integer k2) {
          this.k1 = k1;
          this.k2 = k2;
      }

      @Override
      public int hashCode() {
          return k1.hashCode() + 17 * k2.hashCode();
      }

      @Override
      public boolean equals(Object o) {
          if (o == this) {
              return true;
          }
          if (o == null || !(o instanceof MyKey)) {
              return false;
          }
          MyKey cp = MyKey.class.cast(o);
          return k1.equals(cp.k1) && k2.equals(cp.k2);
      }
  }

  class MyString  {
      private String value;

      public MyString(Integer k1, Integer k2) {
          value=k1+"-"+k2;
      }

      @Override
      public int hashCode() {
          return value.hashCode();
      }

      @Override
      public boolean equals(Object o) {
          return o.equals(value);
      }
  }
  • 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-15T06:09:56+00:00Added an answer on June 15, 2026 at 6:09 am

    The biggest problem you have is the building of Strings, or creating objects just to perform a lookup.

    A way around this is to have a Map or Map values. As your keys are primitives you are better off using the trove library. TObjectIntHashMap and TIntIntHashMap

    e.g.

    TObjectIntHashMap<TIntIntHashMap> map = ...
    int val = map.get(k1).get(k2);
    

    Using this approach no objects are required to create keys or values.

    If you want to pair the keys you can use the follow

    TLongIntHashMap map = ...
    int val = map.get(((long) k1 << 32) | k2);
    

    e.g.

    long key = ((long) k1 << 32) | k2;
    map.adjustOrPut(key, 1, 1); // a counter for this key.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program so far has one purpose, take in two integers(the size of the
This program tries to move a string out of a function and use it
Program returns an error of, expected PWideChar instead of string procedure TForm1.Button1Click(Sender: TObject); var
This program builds a dictionary out of a list based on what two index
This program runs just fine, but for some reason when I ask for an
/*Program to merge to arrays using pointers in descending order, when the first array
program s; type info = record name, surname: string; min, sec: integer; end; arrays
Program: program s; type info = record name, surname: string; min, sek: integer; end;
My program generates two strings and I want them compared by the external diff
Program I'm making has a simple configuration file looking something like this. @overlays =

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.