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

The Archive Base Latest Questions

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

I have a 2D array of Integers. I want them to be put into

  • 0

I have a 2D array of Integers. I want them to be put into a HashMap. But I want to access the elements from the HashMap based on Array Index. Something like:

For A[2][5], map.get(2,5) which returns a value associated with that key. But how do I create a hashMap with a pair of keys? Or in general, multiple keys: Map<((key1, key2,..,keyN), Value) in a way that I can access the element with using get(key1,key2,…keyN).

EDIT : 3 years after posting the question, I want to add a bit more to it

I came across another way for NxN matrix.

Array indices, i and j can be represented as a single key the following way:

int key = i * N + j;
//map.put(key, a[i][j]); // queue.add(key); 

And the indices can be retrevied from the key in this way:

int i = key / N;
int j = key % N;
  • 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-18T06:09:24+00:00Added an answer on June 18, 2026 at 6:09 am

    There are several options:

    2 dimensions

    Map of maps

    Map<Integer, Map<Integer, V>> map = //...
    //...
    
    map.get(2).get(5);
    

    Wrapper key object

    public class Key {
    
        private final int x;
        private final int y;
    
        public Key(int x, int y) {
            this.x = x;
            this.y = y;
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Key)) return false;
            Key key = (Key) o;
            return x == key.x && y == key.y;
        }
    
        @Override
        public int hashCode() {
            int result = x;
            result = 31 * result + y;
            return result;
        }
    
    }
    

    Implementing equals() and hashCode() is crucial here. Then you simply use:

    Map<Key, V> map = //...
    

    and:

    map.get(new Key(2, 5));
    

    Table from Guava

    Table<Integer, Integer, V> table = HashBasedTable.create();
    //...
    
    table.get(2, 5);
    

    Table uses map of maps underneath.

    N dimensions

    Notice that special Key class is the only approach that scales to n-dimensions. You might also consider:

    Map<List<Integer>, V> map = //...
    

    but that’s terrible from performance perspective, as well as readability and correctness (no easy way to enforce list size).

    Maybe take a look at Scala where you have tuples and case classes (replacing whole Key class with one-liner).

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

Sidebar

Related Questions

I have integers under the value of 255 and want to save them into
Possible Duplicate: Java: Reading integers from a file into an array I want to
I have an array with integers of values from 0 to 100. I wish
I have an array of integers and I want to save pairs of integers
For example I have an array of 200 integers. What I want to do
I have a byte array, I want to convert them to integer. How to
Think I have an integer array like this: a[0]=60; a[1]=321; a[2]=5; now I want
I have an array of Integers in Java, I would like use only a
I have an array of integers in my core data model. It is set
I have an array of integers which represent a RGB image and would like

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.