Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes hashpmap and dictionary and hashtable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "hashpmap and dictionary and hashtable?" Watch "hashpmap and dictionary and hashtable?" New topic
Author

hashpmap and dictionary and hashtable?

senthil sen
Ranch Hand

Joined: Oct 10, 2002
Posts: 184
hashpmap,dictionary,hashtable
difference between these is hashtable is synchronised?
Any diferences??
Diference between iteration and enumeration is
iterator have capability to remove object from the collection
Any other difference between these two??
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by senthil sen:
hashpmap,dictionary,hashtable
difference between these is hashtable is synchronised?
Any diferences??
Diference between iteration and enumeration is
iterator have capability to remove object from the collection
Any other difference between these two??
Let me start with a bit of history. Pre-JDK 1.2, all we had was Hashtable, Vector, Stack, and Enumeration. The problem was, they were synchronized which with the benefit of hindsight was a bad idea (more about that in a minute); they were far too limited; and their method names were overly long.
So for JDK 1.2, Sun added the Collections framework. These Collections were done the right way. They aren't synchronized. They are based on a set of interfaces (Collection, List, Map, etc) with well-defined semantics rooted in set theory. And the methods are short, sweet an an awful lot more logical.
To help interoperability between the old and the new classes, Sun retrofitted the old classes (Vector and friends) with the new interfaces (List and friends). This meant that these classes got pretty confusing since there are at least two ways to do the same thing, but it was a small price to pay.
Unfortunately, Sun could not easily deprecate the old classes. They were widely used, among things by many of their own APIs. These APIs (eg Servlets) needed to keep on using the old classes in order to remain compatible with JDK 1.1. Even today these classes are still not deprecated.
Back to today.
Never use the old-style classes -- Hashtable, Vector, Stack, Enumeration -- unless you really have to.
Why?
  • The Collections framework is a consistent, feature-complete set of interfaces and classes that can fulfil every need. The old ones are just needless baggage, extra things to know for no good reason.
  • The API of Vector and friends is confusing. What shall I use today, an iterator() or elements()? If you use Vector in a team context, you can be sure that different developers will use them in a different way.
  • Vector and friends are synchronized which will make your applications slower and less likely to be thread-safe. What, I hear you say, less likely? Yes. Because the synchronization of a Vector (or Hashtable or...) is in most cases in the wrong place; it isn't the little Vector methods that need to be atomic, but much coarser-grained composite methods that you write. The "thread safety" of Vector and friends frequently lures inexperience developers into a false sense of security.
  • I know there are still a lot of developers using Vector and so forth. Well, don't. Distinguish yourself from the rabble and the unwashed masses with a more intelligent choice Use the Collections framework.
    - Peter
    [ February 14, 2003: Message edited by: Peter den Haan ]
    Ellen Zhao
    Ranch Hand

    Joined: Sep 17, 2002
    Posts: 581
    Originally posted by Peter den Haan:
    ...
    They are based on a set of interfaces(Collection, List, Map, etc) with well-defined semantics rooted in set theory. And the methods are short, sweet an an awful lot more logical.

    Nice revealing and rational convincing. Thanks!

    Never use the old-style classes -- Hashtable, Vector, Stack, Enumeration -- unless you really have to.
    Why?...

    Oh gosh, I was using Vector almost all the time and no one had told me that there was so awful trick I remember once I used LinkedList by accident in a data structure implementation, but someone "corrected" that it with Vector... With the reasoning I quoted above, I'll reject such correction later. Thanks again.
    Ellen Zhao
    Ranch Hand

    Joined: Sep 17, 2002
    Posts: 581
    From where did you get such info, Peter? I guess by reading the source code of Java API line by line maybe I could figure out some underlying and then judge what's good and what's less good, but, more than 1500 classes there and it's said there are more than 3000 lines in a single Object class. Currently am a bit lazy to do so...Is there any good resource to know those? The java language specification( haven't read all of it yet )? Thank you.

    Regards,
    Ellen
    Peter den Haan
    author
    Ranch Hand

    Joined: Apr 20, 2000
    Posts: 3252
    My recommendation not to use Vector et al is an opinion, of course -- after all, they are not formally deprecated and given their wide use they probably won't be anytime soon. Yet I believe it to be an opinion based on solid facts.
    And Sun seems to think so, too. The tech tips at the time said with so many words that classes such as Vector are still available, but the new ones are preferred. In the Annotated Outline of the Collections Framework, Vector and friends are headed under "Legacy Implementations". Josh Bloch, too, makes clear in his Collections trail that these classes are not considered part of the framework, and surrounds their use with caveats. I could quote many more sources; suffice to say that during the transitional period when JDK 1.2 came out many intelligent sources expressed the opinion that Vector et al should now go the way of the dodo, and my own experience since only confirmed their wisdom. I'm rather dismayed that the old mistak... I mean classes are still so widely used and will argue against them at every opportunity.
    - Peter
    Thomas Paul
    mister krabs
    Ranch Hand

    Joined: May 05, 2000
    Posts: 13974
    Even if you need a synchronized Collection you shouldn't be using Vector since the Collections class has a method to synchronize any Collection object.


    Associate Instructor - Hofstra University
    Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
    boyet silverio
    Ranch Hand

    Joined: Aug 28, 2002
    Posts: 173
    Way to go Peter, and the others. I think if Peter had not immediately responded to Senthil's questions, Senthil would have again received responses such as "do your homework first blah blah blah" or "take time to read over such basic topics befor asking questions blah blah" from "impatient" people around. Several times I read such responses to his questions.
    To Senthil, way to go also for persisting with such 'basic' questions. Thats one way of learning. There are people like Peter who can provide substantial answers to questions which others may find "too basic".
    senthil sen
    Ranch Hand

    Joined: Oct 10, 2002
    Posts: 184
    thanks guys,
    yeah i ben responded like what boyet as said..
    Even though ur a good programmer with over 2 years in java still u will have small doubts like this.
    As long as guys are here to answers these question i think it would be good for guys like me.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: hashpmap and dictionary and hashtable?
     
    Similar Threads
    HashTable and HashMap
    HashMap confused
    Hashtable allowing null values
    Hashtable vs. HashMap
    Differences