Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why we need Map[Hashtable or hashMap]?

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks.

even we can use a Custom Object[key,value fields] or two dimensional array to key|value fair[instead of Map]. so why we go for Map?. because of hashing function. what is the efficient than other data structure.

Please Clarify my doubt .
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its like why we use ArrayList when we can use array for the same thing, but as per my knowledge the data structure has some important things for me.

1. List/Map size increased as you go on inserting new element, so when you don't have a fixed number of element we can use list/map class.
2. You have better control on elements, like get first, get last element, etc methods.
3. Generics, you can't declare generic array but you can have generic list, maps.

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well sagar , but my question is different. even i can use Custom Object[which has key , value fields] of List[which can grow dynamically] instead of Map
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well you can create the custom class which has key, value as its field, but how you can connect them. You've this Map class doing the same thing for you.
 
PrasannaKumar Sathiyanantham
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can create the custom datastructres by ourselves but it is a cumbersome job.

That's why we have collections.

As to why we use map please look into datastructures book by weiss. A really good book very difficult to understand.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sagar Rohankar wrote: but how you can connect them.


i dont think that it is difficult . obj.getkey().equals("A") then get Value
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Map can provide faster access.

If you use a List you need to loop through the entire List until you get a match (linear searching). With a HashMap, the access is nearly instantaneous - using the hash code a very small subset of entries (called buckets, ideally only one element) is retrieved and that is searched like a List. This is in the end a lot faster than a List.
With TreeMap the access is a bit slower but still faster than linear. The elements are spread out like a binary tree. Each node (also non-leaf nodes) has an element and at most two children. Lookup is determined by starting at the root using this algorithm:
- if the element's match then stop at that element
- if the element is larger than check the left sub tree
- if the element is smaller than check the right sub tree

All in all, for a simple lookup a List is O(n) (linear), a HashMap is O(1) (constant) and a TreeMap is O(log(n)) (logarithmic).
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman:

Why do all that work if someone's already done it for you?

John.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you all . bit cleared
 
Campbell Ritchie
Sheriff
Pie
Posts: 50232
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
seetharaman venkatasamy wrote:thank you all . bit cleared
I think if you look here, it will be cleared up more
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic