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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:37:37+00:00 2026-06-02T04:37:37+00:00

I have the following HashMap with properties keys and values: private HashMap<String, Object> prop_values;

  • 0

I have the following HashMap with properties keys and values:

private HashMap<String, Object> prop_values;

I need to check if one instance of it is equal to another one. In the past, i just did this:

if (prop_values_1.equals(prop_values_2)){
   //  do something
}

And this worked until i got Object[] as a value. So, my previous expression always returned false on such HashMap with any Object[] value.

So, i have to implement this method:

private boolean isPropValuesEquals(HashMap<String, Object> pv1, HashMap<String, Object> pv2){
   boolean isEquals = true;

   if (pv1 == null || pv2 == null){
      return false;
   }

   if (!pv1.keySet().equals(pv2.keySet())){
      return false;
   }

   for (String key : pv1.keySet()){

      Object cur_pv1 = pv1.get(key);
      Object cur_pv2 = pv2.get(key);

      if (cur_pv1 instanceof Object[]){
         if (cur_pv2 instanceof Object[]){
            isEquals = Arrays.equals((Object[])cur_pv1, (Object[])cur_pv2);
         } else {
            return false;
         }
      } else {
         isEquals = isEquals && cur_pv1.equals(cur_pv2);
      }

      if (!isEquals){
         return false;
      }
   }

   return isEquals;

}

It works, but it seems to be some kind of hack, and i’m not sure this is the best way to achieve what I need.

So, here’s two questions:

  • why Object[].equals() is not the same as Arrays.equals()? It seems to be painful.

  • is there some better way to compare HashMap<String, Object>, if values can be an Object[] ?

  • 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-02T04:37:38+00:00Added an answer on June 2, 2026 at 4:37 am

    The deep problem is that an there’s no way to override the equals() of an array. Why it’s not written as “equal elements in the same order” in the first place, I have no idea. It definitely could have been (unless there’s some obscure rationale for not doing it; I can’t think of any; if you wanted to check for reference equality, you use ==, so what would a working equals() harm?).

    Your solution is the way to go. Just a couple of details to consider:

    • Instead of x instanceof Object[], you could use x.getClass().isArray(), so it would work for other arrays as well, such as int[] (which is not a subclass of Object[]). Downside: you may have to separately check if x is null.

    • If the arrays may contain nested arrays, consider using Arrays.deepEquals().

    A demonstration that primitive arrays are not Object[]s:

        Object a = new int[1];
        System.out.println("" + (a instanceof Object[])); // false
        System.out.println("" + a.getClass().isArray()); // true
    

    Yet another pain in the arse is that even if you find that x is an array, you still have to handle separately cases for all the different primitive element types. There’s no way to handle them in a generic way in Java’s type system. Of course, if you don’t have primitive arrays in our map, then you don’t need to handle this case.

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

Sidebar

Related Questions

I have the following code private Map<KEY, Object> values = new HashMap<KEY, Object>(); public
I have the following class: class DerivedMap extends Hashmap<String,Object> {} Which I use because
I have declared the following method: private void mockInvokeDBHandler(Map<String, Object>... rows) { List<Map<String, Object>>
I have the following line of code this.htmlSpecialChars = this.getSpecialCharMap(); where private HashMap<String,String> htmlSpecialChars;
I have the following class: class IndexItem { private String word; private HashMap<String, Integer>
I have following piece of code : public Hashmap<String,String> tempmap = new HashMap<String,String>(); and
If I have a hashmap containing the following: Hashmap contains (String, String) How can
I have the following code: Map<String, ObjectType> objectMap = new HashMap<String, ObjectType>(); for (ObjectType
I have the following code: InputStream reportFile = MyPage.this.getClass().getResourceAsStream(test.jrxml); HashMap<String, String> parameters = new
I have the following Map: Map<String, List<String>> map = new HashMap<String, List<String>>(); which is

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.