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.
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gudii9Author Commented:
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
0
gurpsbassiCommented:
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.
0
gurpsbassiCommented:
As for your second question - it is entirely possible for two different strings to have the same hashcode.
There is nothing in the contract to prevent this so collisions will occur.

Note: - don't use hashcode as a basis for equality. You should use it along with .equals().
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

gudii9Author Commented:
I see the hash values are negative though as attached which is not clear?
HashMapQuestions.png
hashNegative.png
0
gudii9Author Commented:
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
0
CEHJCommented:
how do i know which all strings produce same hashCode value before hand like in this case str1 and str2 has same hashCode?
Why would you need to?

As for collisions, String.hashCode uses quite a primitive hash function, by design. This is for performance reasons.

As for negativity, that's of no consequence, when it's used as it will be turned into a positive value
0
gudii9Author Commented:
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
0
CEHJCommented:
I don't have any stats for that sorry
0
gudii9Author Commented:
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?
0
CEHJCommented:
The best way to understand Java maps is to study the source code. e.g. java.util.HashMap
0
gudii9Author Commented:
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
0
CEHJCommented:
The bucket starts as the tail of a linked list of HashMap.Entry
0
gudii9Author Commented:
8 shown in HashMapQuestions.png screenshot is bucket number or position of hash map?
0
mccarlIT Business Systems Analyst / Software DeveloperCommented:
8 shown in HashMapQuestions.png screenshot is bucket number or position of hash map?
A Map doesn't have a notion of "position" not like a List, eg. for a List you can ask, "What is the 8th element in this list?" via a List.get(8) method call. But for a Map, the entries are not ordered in any meaningful way, so you can't ask the same question and there is no method Map.get(int position) available to call on a Map.

So, I would call it bucket number 8.
0
gudii9Author Commented:
Oh
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.