• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

why we need Map[Hashtable or hashMap]?

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you all . bit cleared
 
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic