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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:36:36+00:00 2026-05-24T10:36:36+00:00

I have a Hashmap which has X number of elements I need to move

  • 0

I have a Hashmap which has X number of elements
I need to move this map into another map
This is what my code looks like

Map originMap = initialize();
Map destMap = new Hashmap ();  

int originMapSize = originMap.size(); 
Set<Map.Entry<K, V>> entries = originMap.entrySet();
for (Map.Entry<K, Y> mapEntry : entries) {
 K key = mapEntry.getKey();
 V value = mapEntry.getValue();
 destMap.put (key,value);
}  

// Shouldnt this be equal to originMapSize ????
int destMapSize = destMap.size();

What I am observing is – originMapSize is NOT equal to the destMapSize

It seems when we put the elements in the destMap, some of the elements are being overridden

We have overrridden the hashCode and equals method- and it is a suspicious implementation.
However, if the originMap allowed the elements to be added, why would the destinationMap not add a new elements and override an existing element instead ?

  • 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-24T10:36:36+00:00Added an answer on May 24, 2026 at 10:36 am

    This could happen if the equals method was asymmetric. Suppose there are two keys a and b such that:

    • a.hashCode() == b.hashCode()
    • a.equals(b) returns false
    • b.equals(a) returns true

    Then suppose that the HashMap implementation searches for an existing key by calling existingKey.equals(newKey) for each existing key with the same hash code as the new key.

    Now suppose we originally add them in the order { a, b }.

    The first key (a) obviously goes in with no problems. The second key (b) insertion ends up calling a.equals(b) – which is false, so we get two keys.

    Now building the second HashMap, we may end up getting the entries in the order { b, a }.

    This time we add b first, which is fine… but when we insert the second key (a) we end up calling b.equals(a), which returns true, so we overwrite the entry.

    That may not be what’s going on, but it could explain things – and shows the dangers of an asymmetric equals method.

    EDIT: Here’s a short but complete program demonstrating this situation. (The exact details of a and b may not be the same, but the asymmetry is.)

    import java.util.*;
    
    public class Test {
    
        private final String name;
    
        public Test(String name)
        {
            this.name = name;
        }
    
        public static void main(String[] args)
        {
            Map<Test, String> firstMap = new HashMap<Test, String>();
    
            Test a = new Test("a");
            Test b = new Test("b");
    
            firstMap.put(b, "b");
            firstMap.put(a, "a");
    
            Map<Test, String> secondMap = new HashMap<Test, String>();
            for (Map.Entry<Test, String> entry : firstMap.entrySet())
            {
                System.out.println("Adding " + entry.getKey().name);
                secondMap.put(entry.getKey(), entry.getValue());
            }
            System.out.println(secondMap.size());
        }
    
        @Override public int hashCode()
        {
            return 0;
        }
    
        @Override public boolean equals(Object other)
        {
            return this.name.equals("b");
        }
    }
    

    Output on my machine:

    Adding a
    Adding b
    1
    

    You may not get the output that way round – it depends on:

    • The way that equals is called (candidateKey.equals(newKey) or vice versa)
    • The order in which entries are returned from the set

    It may even work differently on different runs.

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

Sidebar

Related Questions

I have a HashMap<String,String> and there is a static method which returns this map
I have bean MyBean, which has property HashMap - map which values type is
I have this HashMap: HashMap<String, Integer> m which basically stores any word (String) and
I have a List of HashMap 's which has key of type Integer and
I have this code which applies a counter to each item on the list.
I have code that has a Map of (Message)Handlers. I'm trying to make the
I have HashMap 1, which contains 5 keys, all of which have Hashmaps as
If I have the value foo , and a HashMap<String> ftw for which ftw.containsValue(foo)
I have a HashMap (although I guess this question applies to other collections) of
I have a HashMap relating Keys to Strings, and I need to compare some

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.