Avatar of gudii9
gudii9
Flag for United States of America asked on

hashmap related questions

I am checking below link on hashmap

https://www.youtube.com/watch?v=iinE6ZBNBjw

package test;

import java.util.HashMap;

public class TestHashmap {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashMap<String, Integer> map = new HashMap<>();
		map.put("a1", 1);
		map.put("a2", 2);
		map.put("a3", 3);
		map.put("a4", 4);
		System.out.println("map--" + map);
		//String str1
	}

}

Open in new window



when i put 'a1' why it put in 8th bucket out of all 16 buckets rather than 1st. Why it put second element in 9th bucket.
JavaProgramming Languages-OtherProgramming

Avatar of undefined
Last Comment
gudii9

8/22/2022 - Mon
gudii9

ASKER
package test;

import java.util.HashMap;

public class TestHashmap {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		HashMap<String, Integer> map = new HashMap<>();
		map.put("a1", 1);
		map.put("a2", 2);
		map.put("a3", 3);
		map.put("a4", 4);
		System.out.println("map--" + map);
		String str1="abcdef";
		String str2="abcdfG";
		String str3="abcdefH";
		System.out.println("str1 hashcodee"+str1.hashCode());
		System.out.println("str2 hashcodee"+str2.hashCode());
		System.out.println("str3 hashcodee"+str3.hashCode());
	}

}

Open in new window


why the hashCode is same for str1 and str2?
map--{a1=1, a2=2, a3=3, a4=4}
str1 hashcodee-1424385949
str2 hashcodee-1424385949
str3 hashcodee-1206291387
gurpsbassi

When you say 8th bucket, how do you know its gone into the 8th bucket?
bucket is not the same as the position in the hashmap. A bucket is a collection of entries.
ASKER CERTIFIED SOLUTION
gurpsbassi

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gudii9

ASKER
I see the hash values are negative though as attached which is not clear?
HashMapQuestions.png
hashNegative.png
Your help has saved me hundreds of hours of internet surfing.
fblack61
gudii9

ASKER
how do i know which all strings produce same hashCode value before hand like in this case str1 and str2 has same hashCode?

map--{a1=1, a2=2, a3=3, a4=4}
str1 hashcodee-1424385949
str2 hashcodee-1424385949
str3 hashcodee-1206291387
SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gudii9

ASKER
Why would you need to?
just general curiosity.

What all single digit string, 2digit string (like AA, DC, ZX etc) , 3 digit string like (AAA, FRG,EWA etc) have same hash?
Any program for that to find? what percentage times these hash values are equal?
please advise
CEHJ

I don't have any stats for that sorry
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gurpsbassi

gudii9

ASKER
http://www.enterjava.com/search?updated-min=2014-01-01T00:00:00%2B05:30&updated-max=2015-01-01T00:00:00%2B05:30&max-results=10

The put method internally uses the hashcode of the object and calculates the hash and indexFor to find the bucket number.

It has inserted in bucket 9. When you use containsKey function , it uses the hashing functionality to find the bucket number.

About bucket selection i see link explained about containsKey  and put methods which decides whether 9 or 8th bucket. I was not clear on that. can you please advise more that?
CEHJ

The best way to understand Java maps is to study the source code. e.g. java.util.HashMap
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
gudii9

ASKER
When you say 8th bucket, how do you know its gone into the 8th bucket?
bucket is not the same as the position in the hashmap. A bucket is a collection of entries.

as attached i see 8. Not sure if it is position of hashmap or bucket. I guess i am thinking both as same. I wonder how bucket and position are different. please advise
HashMapQuestions.png
CEHJ

The bucket starts as the tail of a linked list of HashMap.Entry
gudii9

ASKER
8 shown in HashMapQuestions.png screenshot is bucket number or position of hash map?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
mccarl

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
gudii9

ASKER
Oh