G
gaurav v bagga
hi all,
I have key,value pair of structure and I want to store them in a
collection. Which collection will be good with respect to retrieval
speed.
I found out from net that HashMap is better then TreeMap in retrieval
speed.
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("1", "r");
hm.put("2", "r");
hm.put("3", "r");
hm.put("4", "r");
hm.put("5", "r");
hm.put("6", "r");
hm.put("7", "rd");
hm.put("8", "r");
hm.put("9", "r");
hm.put("10", "r");
hm.put("11", "r");
hm.put("12", "r");
hm.put("13", "r");
hm.put("14", "rd");
long time=System.nanoTime();
System.out.println(hm.get("10"));
System.out.println(System.nanoTime()-time);
TreeMap<String, String> tm = new TreeMap<String, String>();
tm.put("1", "r");
tm.put("2", "r");
tm.put("3", "r");
tm.put("4", "r");
tm.put("5", "r");
tm.put("6", "r");
tm.put("7", "rd");
tm.put("8", "r");
tm.put("9", "r");
tm.put("10", "r");
tm.put("11", "r");
tm.put("12", "r");
tm.put("13", "r");
tm.put("14", "rd");
time=System.nanoTime();
System.out.println(tm.get("10"));
System.out.println(System.nanoTime()-time);
but this code of mine gives result as
r
2405892
r
832229
it seems tree is better or is it that I am measuring the speed in wrong
way?
regards
gaurav
I have key,value pair of structure and I want to store them in a
collection. Which collection will be good with respect to retrieval
speed.
I found out from net that HashMap is better then TreeMap in retrieval
speed.
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("1", "r");
hm.put("2", "r");
hm.put("3", "r");
hm.put("4", "r");
hm.put("5", "r");
hm.put("6", "r");
hm.put("7", "rd");
hm.put("8", "r");
hm.put("9", "r");
hm.put("10", "r");
hm.put("11", "r");
hm.put("12", "r");
hm.put("13", "r");
hm.put("14", "rd");
long time=System.nanoTime();
System.out.println(hm.get("10"));
System.out.println(System.nanoTime()-time);
TreeMap<String, String> tm = new TreeMap<String, String>();
tm.put("1", "r");
tm.put("2", "r");
tm.put("3", "r");
tm.put("4", "r");
tm.put("5", "r");
tm.put("6", "r");
tm.put("7", "rd");
tm.put("8", "r");
tm.put("9", "r");
tm.put("10", "r");
tm.put("11", "r");
tm.put("12", "r");
tm.put("13", "r");
tm.put("14", "rd");
time=System.nanoTime();
System.out.println(tm.get("10"));
System.out.println(System.nanoTime()-time);
but this code of mine gives result as
r
2405892
r
832229
it seems tree is better or is it that I am measuring the speed in wrong
way?
regards
gaurav