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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:44:37+00:00 2026-05-30T19:44:37+00:00

I am using a HashMap data structure to store a SqMatrix (square matrix), where

  • 0

I am using a HashMap data structure to store a SqMatrix (square matrix), where the key is of type MatrixIndex (which contains row and col) and the value is of type Integer.

But when I am getting false as output of “if (mat.containsKey(key))” though the HashMap has the corresponding key in it.

The main code:

public static void main(String[] args) {

    Random generator = new Random();
    int val = 0;
    Types.MatrixIndex key, key1;
    int matSz = (int) Math.floor(Math.sqrt(10));
    Types.SqMatrix mat = new Types().new SqMatrix(matSz); //matSz*matSz elements
    //HashMap<Types.MatrixIndex,Integer> hMap= new HashMap<Types.MatrixIndex,Integer>(10);
    for (int r=0; r<matSz; r++) {
        for (int c=0; c<matSz; c++) {
            if (r<c) {
                val = generator.nextInt(2) > 0? -1 : val;
                key =(new Types()).new MatrixIndex(r, c);
                key1 = (new Types()).new MatrixIndex(c, r);
                mat.put(key, val);
                mat.put(key1, val);
                generator.setSeed(System.currentTimeMillis());
            }
        }
    }

    for (int r=0; r<matSz; r++) {
        val = 0;
        for (int c=0; c<matSz; c++) {
            if (r!=c) {
                key = (new Types()).new MatrixIndex(r, c);
                if (mat.containsKey(key)) {
                    val = val + mat.get(key);
                }

            }
        }
        key1 = (new Types()).new MatrixIndex(r, r);
        mat.put(key1, val);
    }

Do anybody have an idea on why the containsKey is returning false though it is present in the HashMap?

Thanks in advance,

Somnath

  • 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-30T19:44:39+00:00Added an answer on May 30, 2026 at 7:44 pm

    I have no idea what MatrixIndex is, but if it’s hashcode implementation isn’t overridden, then every instance of MatrixIndex has it’s own hashcode, and is considered unique. Therefore, you can’t pass in a new instance of MatrixIndex to do a containsKey() check.

    If you want that exact code snippet to work, you need to override equals() and hashcode() in your MatrixIndex class, and make it so that it always creates a reproducible, unique value for that class.

    If you haven’t done this before, definitely read up on overriding these two methods. A quick search will bring up a great deal of help. If you can add new libraries to your project, look at: http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/builder/HashCodeBuilder.html

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

Sidebar

Related Questions

I'm building a thesaurus using a HashMap to store the synonyms. I'm trying to
I was thinking of using a Double as the key to a HashMap but
i'm using jdbc to work with my db. I want to store a HashMap
I have a problem putting data into an unordered_map using a struct as key:
Here is my situation. I am using two java.util.HashMap to store some frequently used
I need some data structure that I can build from standard collections or using
I search for a datastructure, where I can store several key-value pairs. The data
I'm using ghc 6.12.2 and the latest Data.HashMap 1.1.0 package, and while compiling with
In java while using the HashMap, they are using the Iterator class. But I
I'm building a thesaurus using a HashMap <String,ArrayList<String>> to hold words and their synonyms

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.