TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to JAVA

Java Hashtable

Use the Hashtable collection from java.util.Hashtable. Benchmark Hashtable and HashMap.
Hashtable. This is an associative array: it associates keys (of any type) to values (of any type). HashMap is similar to Hashtable but not thread safe. It is slower.HashMap
Not preferred. With Hashtable, performance is reduced by internal synchronization (thread-checking) code. This makes it about 200% slower than HashMap on simple lookups (with containsKey).
Sample usage. This program shows how to create a new Hashtable with String keys, Integer values. We can use any class type for the keys and values. We use put and get.

Put: This assigns a new entry in the Hashtable. The key is the first argument. The value is the second.

Get: This looks up a value in the Hashtable from the key. Internally it computes a hash code from the key for faster searching.

Java program that uses Hashtable import java.util.Hashtable; public class Program { public static void main(String[] args) { // Create Hashtable and add two entries. Hashtable<String, Integer> hash = new Hashtable<>(); hash.put("cat", 10); hash.put("dog", 15); // Get value at this key. int value = hash.get("cat"); System.out.println(value); } } Output 10
Benchmark, Hashtable. This program benchmarks the Hashtable against HashMap. We first initialize the 2 collections with put() method calls.

Version 1: This version of the code uses the Hashtable. It checks the collection for the string key "apple."

Version 2: This code does the same thing as version 1, but uses a HashMap collection instead.

Result: The HashMap is faster, for simple lookups, by about 200%. Using HashMap when possible is an optimization.

Java program that benchmarks Hashtable, HashMap import java.util.HashMap; import java.util.Hashtable; public class Program { public static void main(String[] args) throws Exception { Hashtable<String, Integer> table = new Hashtable<>(); table.put("carrot", 10); table.put("apple", 20); HashMap<String, Integer> map = new HashMap<>(); map.put("carrot", 10); map.put("apple", 20); long t1 = System.currentTimeMillis(); // Version 1: check Hashtable. for (int i = 0; i < 1000000; i++) { if (!table.containsKey("apple")) { throw new Exception(); } } long t2 = System.currentTimeMillis(); // Version 2: check HashMap. for (int i = 0; i < 1000000; i++) { if (!map.containsKey("apple")) { throw new Exception(); } } long t3 = System.currentTimeMillis(); // ... Times. System.out.println(t2 - t1); System.out.println(t3 - t2); } } Output 53 17
In most programs, a HashMap is the better choice. If we have complex threading requirements, Hashtable may be necessary. HashMap has a significant performance advantage.
Retrofitted. Over the years, Hashtable has been retrofitted to support modern interfaces and generics in Java. And HashMap was introduced to provide an entirely new, faster option.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf