Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: difference between hashtable and hashMap ? Date: Sun, 24 Aug 2014 08:13:03 -0400 Organization: A noiseless patient Spider Lines: 31 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 24 Aug 2014 12:13:09 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="bd4e1cba8d8592bec5d5ed9e4537fbfd"; logging-data="20269"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+fZWzqPl3aJn+/Ce7GEDAr" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.0 In-Reply-To: Cancel-Lock: sha1:CozRNzszgeMSivEEDMvfa1o++sE= Xref: csiph.com comp.lang.java.programmer:29835 On 8/24/2014 2:03 AM, Rishipal Singh wrote: > what is difference between hashtable and hashMap ; > please explain with examples If by "hashtable" you actually mean "HashTable", the latter is probably the class "java.util.HashTable", a key-to-value map that has been around since the very first versions of Java. If by "hashMap" you actually mean "HashMap", the latter is probably the class "java.util.HashMap", another key-to-value map of more recent (Java 1.2, 1998) vintage. Both are used in similar ways: You can add and remove key/value pairs, offer a key and retrieve its associated value, change the value associated with a key, iterate over all the keys, all the values, or all the pairs, and so on. There are differences in detail, most notably that HashTable methods are "synchronized" while HashMap methods are not, but the two are broadly similar. Prefer HashMap unless you have a compelling reason to choose HashTable instead -- for example, if you're using an old API that specifically requires HashTable, you have no choice and must use one. But for new work, choose HashMap instead. Also, if your classes expose a key/value map to their clients, prefer to expose it as a Map even if you actually use HashMap internally; that way, you can change to a different kind of map later on (if that seems a good idea) without disturbing your clients. -- esosman@comcast-dot-net.invalid