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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:06:15+00:00 2026-05-14T05:06:15+00:00

I am using this code to check that array is present in the HashMap

  • 0

I am using this code to check that array is present in the HashMap:

public class Test {
    public static void main(String[] arg) {
        HashMap<int[], String> map = new HashMap<int[], String>();
        map.put(new int[]{1, 2}, "sun");
        System.out.println(map.containsKey((new int[]{1, 2})));
    }
}

But this prints False. How can I check that array is present in the HashMap?

  • 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-14T05:06:15+00:00Added an answer on May 14, 2026 at 5:06 am

    The problem is because the two int[] aren’t equal.

    System.out.println(
        (new int[] { 1, 2 }).equals(new int[] { 1, 2 })
    ); // prints "false"
    

    Map and other Java Collections Framework classes defines its interface in terms of equals. From Map API:

    Many methods in Collections Framework interfaces are defined in terms of the equals method. For example, the specification for the containsKey(Object key) method says: “returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k)).”

    Note that they don’t have to be the same object; they just have to be equals. Arrays in Java extends from Object, whose default implementation of equals returns true only on object identity; hence why it prints false in above snippet.


    You can solve your problem in one of many ways:

    • Define your own wrapper class for arrays whose equals uses java.util.Arrays equals/deepEquals method.
      • And don’t forget that when you @Override equals(Object), you must also @Override hashCode
    • Use something like List<Integer> that does define equals in terms of the values they contain
    • Or, if you can work with reference equality for equals, you can just stick with what you have. Just as you shouldn’t expect the above snippet to ever print true, you shouldn’t ever expect to be able to find your arrays by its values alone; you must hang-on to and use the original references every time.

    See also:

    • Overriding equals and hashCode in Java
    • How to ensure hashCode() is consistent with equals()?
    • Understanding the workings of equals and hashCode in a HashMap

    API

    • Object.equals and Object.hashCode
      • It’s essential for a Java programmer to be aware of these contracts and how to make them work with/for the rest of the system
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this code to check if the request came from a page
i am using this code to check if my glsl shader compiled fine. glGetObjectParameterivARB(obj,
Ok I am using Sonar to check code quality. It's telling me this simple
Using this code var html='<div class=somediv></div>'; var target=document.getElementById('contentArea'); target.appendChild(html); I'm getting Uncaught Error: NOT_FOUND_ERR:
Im using this code: protected String getContactInfo() { Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, null,
I was inserting multiple arrays into my database using this code: $queries_cols = array();
So I have this array list that is populated using a csv file. Once
I have list of items with check boxes using this code: ProcessList = (ListView)
Using this code, i am able to send a notification to my own device.
Using this code to zip a folder and it works perfect on small files

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.