hashmap related questions

gudii9
gudii9 used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

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
Top Expert 2015

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.
Top Expert 2015
Commented:
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().
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
I see the hash values are negative though as attached which is not clear?
HashMapQuestions.png
hashNegative.png

Author

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
Top Expert 2016
Commented:
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

Author

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
Top Expert 2016

Commented:
I don't have any stats for that sorry
Top Expert 2015

Commented:

Author

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?
Top Expert 2016

Commented:
The best way to understand Java maps is to study the source code. e.g. java.util.HashMap

Author

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
Top Expert 2016

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

Author

Commented:
8 shown in HashMapQuestions.png screenshot is bucket number or position of hash map?
mccarlIT Business Systems Analyst / Software Developer
Top Expert 2015
Commented:
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.

Author

Commented:
Oh

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial