aspose file tools*
The moose likes Beginning Java and the fly likes why we need Map[Hashtable or hashMap]? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why we need Map[Hashtable or hashMap]?" Watch "why we need Map[Hashtable or hashMap]?" New topic
Author

why we need Map[Hashtable or hashMap]?

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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.


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: Nov 12, 2009
Posts: 110
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.


To err is human,
To forgive is not company policy
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Oct 27, 2005
Posts: 19761
    
  20

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).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Seetharaman:

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

John.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

thank you all . bit cleared
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39855
    
  28
seetharaman venkatasamy wrote:thank you all . bit cleared
I think if you look here, it will be cleared up more
 
Consider Paul's rocket mass heater.
 
subject: why we need Map[Hashtable or hashMap]?