import java.util.HashMap;
import java.util.Map.Entry;
public class TestString {
public static void main(String[] args) {
System.gc();
String str = "deepak";
int length = str.length();
System.out.println("str " + str + " length " + length);
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
for(int i=0; i<length; i++){
char ch = str.charAt(i);
if(map.containsKey(ch)){
Integer val = map.get(ch);
map.put(ch, val+1);
}else{
map.put(ch, 1);
}
}
for (Entry<Character, Integer> entry : map.entrySet())
{
int hashCode = entry.hashCode();
char key = entry.getKey();
// int hash = hash();
System.out.println("hashcode " + hashCode + " hashcode of key>> " + entry.getKey().hashCode() + " key : " + key);
}
System.out.println(">>> " + map);
}
}
Output :
str deepak length 6hashcode 113 hashcode of key>> 112 key : p
hashcode 96 hashcode of key>> 97 key : a
hashcode 101 hashcode of key>> 100 key : d
hashcode 103 hashcode of key>> 101 key : e
hashcode 106 hashcode of key>> 107 key : k
>>>
{p=1, a=1, d=1, e=2, k=1}
Can anyone help me to understand the 2 things from the program and output:
The data printed by map object, how it decide the sequence internally? eg. it is printing sequence p, a, d, e, k.
What is the difference in entry.hashcode() and entry.key().hashcode()? Please refer the output to explain the difference.