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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:04:54+00:00 2026-06-10T21:04:54+00:00

I am using java.util.HashMap for association between a contact Name and a list of

  • 0

I am using java.util.HashMap for association between a contact Name and a list of phone numbers associated with the contact so it is of type String, List<String>. However there can be more than one keys that can map to the the same bucket correct as in Say key1:<1234,5678> is stored in index 2 in the hash Map. I could have another key2 that can hash into the same index. So, will the key1:<1234,5678> be replaced by key2:<7890,1456> ???? Or will it be chained and both key1 and key2 will be stored in that index ?

EDIT:
I’m trying to understand this, the code below only returns New Mexico. Now in this case both they both get the same hashcode so this is a collision correct ? In that case shouldn’t both the values be chained ? So shouldn’t the storage look like say hashcode is 2 then at the index of 2 in the map (OR array) it should be 1-Mexico, 1-New mexico correct? So the values returned should be of Mexico and New mexico ? Why isn’t it getting chained here ?

public static void main(String[] args)
    {
        Map<Integer,String> map = new HashMap<Integer,String>();
        map.put(1, "Mexico");
        map.put(1, "New Mexico");
        System.out.println(map.get(1));

    }
  • 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-10T21:04:55+00:00Added an answer on June 10, 2026 at 9:04 pm

    I’m not totally sure I understand your question.

    In a standard HashMap, if you put another value with the same key, it replaces the first one.

    But you may use for example a MultiMap (from Apache commons) to store more than one value per key.

    If your question is related to more than one key having the same index, this is handled by the implementation : the keys for are stored in linked lists (one for each bucket) and real comparisons (using equals) are done on get and put operations so that no collision can happen as long as equals returns false.

    As with any collection related problem, the important thing to do if you intend to use your own class is to correctly implement the equals and hashcode methods (read this).

    Regarding your question in edit :

    This is a feature of HashMap that the first value with a given key is erased by the new one. So “Mexico” is removed from the HashMap when you add “New Mexico”.

    If you want to have more than one value for a given key, use a MultiMap (or simply use a HashMap<Integer, List<String>> but the put operation is a little more tedious).

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

Sidebar

Related Questions

I am creating a class that implements java.util.List . My class is using E
Here is my situation. I am using two java.util.HashMap to store some frequently used
How can I store an ArrayList and/or a HashMap variable using java.util.properties ? If
When using java.util.logging, the log level for a specific class (i.e. for a child
I am using java.util.Timer class and I am using its schedule method to perform
I'm trying to modify a file in a zip-file using java.util.net As direct modification
I'm facing a problem that seems to have no straighforward solution. I'm using java.util.Map
I am using void java.util.Timer.scheduleAtFixedRate(TimerTask task, long delay, long period) to execute a task
An instance of java.util.Properties is passed to me which has been created using: [...]
Say I have the following config for the java util logger I am using

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.