Title: Java Dictionary example - Java Code Geeks
Open Graph Title: Java Dictionary example - Java Code Geeks
Description: In this tutorial we will discuss about dictionaries in Java. A Dictionary is an abstract class that maps keys to values. Every key is associated with a
Open Graph Description: In this tutorial we will discuss about dictionaries in Java. A Dictionary is an abstract class that maps keys to values. Every key is associated with a
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/java-development/java-basics/data-types/java-dictionary-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/java-development/java-basics/data-types/java-dictionary-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/java-development/java-basics/data-types/java-dictionary-example/"},"author":{"name":"Sotirios-Efstathios Maneas","@id":"https://examples.javacodegeeks.com/#/schema/person/03673b064b32807176478bd144079be1"},"headline":"Java Dictionary example","datePublished":"2014-02-07T20:19:52+00:00","dateModified":"2014-02-07T20:30:07+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/java-development/java-basics/data-types/java-dictionary-example/"},"wordCount":515,"commentCount":0,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/java-development/java-basics/data-types/java-dictionary-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","keywords":["hashmap","hashtable","linkedhashmap"],"articleSection":["data types"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/java-development/java-basics/data-types/java-dictionary-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/java-development/java-basics/data-types/java-dictionary-example/","url":"https://examples.javacodegeeks.com/java-development/java-basics/data-types/java-dictionary-example/","name":"Java Dictionary example - Java Code Geeks","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/java-development/java-basics/data-types/java-dictionary-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/java-development/java-basics/data-types/java-dictionary-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2014-02-07T20:19:52+00:00","dateModified":"2014-02-07T20:30:07+00:00","description":"In this tutorial we will discuss about dictionaries in Java. A Dictionary is an abstract class that maps keys to values. Every key is associated with a","breadcrumb":{"@id":"https://examples.javacodegeeks.com/java-development/java-basics/data-types/java-dictionary-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/java-development/java-basics/data-types/java-dictionary-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/java-development/java-basics/data-types/java-dictionary-example/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/java-development/java-basics/data-types/java-dictionary-example/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Java Basics","item":"https://examples.javacodegeeks.com/category/java-development/java-basics/"},{"@type":"ListItem","position":4,"name":"data types","item":"https://examples.javacodegeeks.com/category/java-development/java-basics/data-types/"},{"@type":"ListItem","position":5,"name":"Java Dictionary example"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/03673b064b32807176478bd144079be1","name":"Sotirios-Efstathios Maneas","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Stathis-Maneas-96x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Stathis-Maneas-96x96.jpg","caption":"Sotirios-Efstathios Maneas"},"description":"Sotirios-Efstathios (Stathis) Maneas is a PhD student at the Department of Computer Science at the University of Toronto. His main interests include distributed systems, storage systems, file systems, and operating systems.","sameAs":["https://www.cs.utoronto.ca/~smaneas/","https://www.facebook.com/stathis.maneas","https://linkedin.com/in/smaneas"],"url":"https://examples.javacodegeeks.com/author/stathis-maneas/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Java Basics","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/java-basics\/"}},{"@type":"ListItem","position":4,"item":{"name":"data types","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/java-basics\/data-types\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2014-02-07T22:19:52+02:00","datePublished":"2014-02-07T22:19:52+02:00","dateModified":"2014-02-07T22:30:07+02:00","headline":"Java Dictionary example","name":"Java Dictionary example","keywords":"hashmap,hashtable,linkedhashmap","url":"https:\/\/examples.javacodegeeks.com\/java-development\/java-basics\/data-types\/java-dictionary-example\/","description":"In this tutorial we will discuss about dictionaries in Java. A Dictionary is an abstract class that maps keys to values. Every key is associated with a unique value and key are unique. Any non-null ob","copyrightYear":"2014","articleSection":"data types","articleBody":"In this tutorial we will discuss about dictionaries in Java. A Dictionary is an abstract class that maps keys to values. Every key is associated with a unique value and key are unique. Any non-null object can be used for either a key or a value. An attempt to insert either a null key or a null value to a dictionary will result to a NullPointerException.\r\n\r\nHowever, the original Dictionary class is now deprecated and instead, all new implementations should implement the Map interface. The Map interface provides the functionality of a dictionary, using exactly the same semantics. A Map can provide three views, which allow the contents of the map to be viewed as a set of keys, collection of values, or set of key-value mappings. Finally, some implementations of the Map interface maintain an order among its values.\r\n\r\nThe Map Interface\r\n\r\nA map has the form Map <K, V> where:\r\n\r\n\tK: specifies the type of keys maintained in this map.\r\n\tV: defines the type of mapped values.\r\n\r\n\r\nFurthermore, the\u00a0Map\u00a0interface provides a set of methods that must be implemented. In this section, we will present some of the most fundamental methods of a map:\r\n\r\n\tcontainsKey: Returns true if the map contains the requested key.\r\n\tcontainsValue: Returns true if the map contains the requested value.\r\n\tget: Retrieve the value of the requested key.\r\n\tkeySet: Returns a\u00a0Set\u00a0that contains all keys of the map.\r\n\tput: Adds the requested key-value pair in the map.\r\n\r\n\r\nThe\u00a0Map\u00a0interface is implemented by different Java classes, such as\u00a0HashMap, Hashtable and\u00a0LinkedHashMap. These classes are able to provide the full functionality of a dictionary. However, these classes differ in some key aspects, as presented below:\r\n\r\n\r\n\r\n\r\n\r\nNull Keys\r\n\r\n\r\nNull Values\r\n\r\n\r\nOrder\r\n\r\n\r\nSynchronized\r\n\r\n\r\n\r\nHashMap\r\n\r\nPermitted\r\n\r\n\r\nPermitted\r\n\r\n\r\nNo\r\n\r\n\r\nNo\r\n\r\n\r\n\r\nHashTable\r\n\r\nProhibited\r\n\r\n\r\nProhibited\r\n\r\n\r\nNo\r\n\r\n\r\nYes\r\n\r\n\r\n\r\nLinkedHashMap\r\n\r\nPermitted\r\n\r\n\r\nPermitted\r\n\r\n\r\nYes\r\n\r\n\r\nNo\r\n\r\n\r\n\r\n\r\n\r\nHashTable - HashMap\r\nThe Hashtable class implements a hash table and maps keys to values. A\u00a0HashMap\u00a0is a hash table based implementation of the Map interface. They both contain two fundamental parameters: initial capacity and performance. The capacity is defined as the number of buckets in the hash table, while the load factor is a measure that indicates the maximum value the hash table can reach, before being automatically increased.\r\n\r\nAn example that uses a HashMap\u00a0as a dictionary is shown below. The program can also be executed properly if we change the type of our map to a Hashtable:\r\n\r\nCountWords_v1.java:\r\n\r\nimport java.io.BufferedReader;\r\nimport java.io.File;\r\nimport java.io.FileReader;\r\nimport java.io.IOException;\r\nimport java.util.HashMap;\r\nimport java.util.Hashtable;\r\nimport java.util.Map;\r\n\r\npublic class CountWords_v1 {\r\n\t\r\n\tpublic static void main(String[] args) throws IOException {\r\n\t\tBufferedReader reader = new BufferedReader(new FileReader(new File(\"input.txt\")));\r\n\t\tString inputLine = null;\r\n\t\tMap dictionary = new HashMap();\r\n\t\t\/\/Map dictionary = new Hashtable();\r\n\t\t\r\n\t\twhile((inputLine = reader.readLine()) != null) {\r\n\t\t\t\/\/ Split the input line.\r\n\t\t\tString[] words = inputLine.split(\"\\\\s+\");\r\n\t\t\t\r\n\t\t\t\/\/ Ignore empty lines.\r\n\t\t\tif(inputLine.equals(\"\"))\r\n\t\t\t\tcontinue;\r\n\t\t\t\r\n\t\t\tfor(String word: words) {\r\n\t\t\t\t\/\/ Remove any commas and dots.\r\n\t\t\t\tword = word.replace(\".\", \"\");\r\n\t\t\t\tword = word.replace(\",\", \"\");\r\n\t\t\t\t\r\n\t\t\t\tif(dictionary.containsKey(word)) {\r\n\t\t\t\t\tInteger val = dictionary.get(word);\r\n\t\t\t\t\tdictionary.put(word, val + 1);\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t\tdictionary.put(word, 1);\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\t\/\/ Printing all words stored in the map.\r\n\t\tfor(String key: dictionary.keySet())\r\n\t\t\tSystem.out.println(key + \": \" + dictionary.get(key));\r\n\t\t\r\n\t\t\r\n\t\treader.close();\r\n\t}\r\n}\r\n\r\n\r\nIn this example we used a HashMap\u00a0to store the words of a file and how many times each word appears in that file.\r\n\r\nA sample execution is shown below:\r\nto: 2\r\nGeeks: 1\r\nHashMaps: 1\r\nis: 2\r\ntext: 1\r\na: 1\r\nAlso: 1\r\nHashtables: 1\r\nfrom: 1\r\nLinkedHashMaps: 1\r\nthe: 2\r\nnamely: 1\r\nMaps: 1\r\nused: 1\r\nCode: 1\r\nThis: 1\r\nJava: 2\r\nand: 1\r\nhello: 1\r\nthat: 1\r\npresent: 1\r\nof: 2\r\npower: 2\r\neverybody: 1\r\nsample: 1\r\n\r\nLinkedHashMap\r\nThe LinkedHashMap class provides an implementation of a map that has a predictable iteration order.\r\n\r\nThe same example that counts the references of a word in a file and stores the key-value pairs in a LinkedHashMap is shown below:\r\n\r\nCountWords_v2.java:\r\n\r\nimport java.io.BufferedReader;\r\nimport java.io.File;\r\nimport java.io.FileReader;\r\nimport java.io.IOException;\r\nimport java.util.Iterator;\r\nimport java.util.LinkedHashMap;\r\nimport java.util.Map.Entry;\r\nimport java.util.Set;\r\n\r\npublic class CountWords_v2 {\r\n\t\r\n\tpublic static void main(String[] args) throws IOException {\r\n\t\tBufferedReader reader = new BufferedReader(new FileReader(new File(\"input.txt\")));\r\n\t\tString inputLine = null;\r\n\t\tLinkedHashMap dictionary = new LinkedHashMap();\r\n\t\t\r\n\t\twhile((inputLine = reader.readLine()) != null) {\r\n\t\t\t\/\/ Split the input line.\r\n\t\t\tString[] words = inputLine.split(\"\\\\s+\");\r\n\t\t\t\r\n\t\t\t\/\/ Ignore empty lines.\r\n\t\t\tif(inputLine.equals(\"\"))\r\n\t\t\t\tcontinue;\r\n\t\t\t\r\n\t\t\tfor(String word: words) {\r\n\t\t\t\t\/\/ Remove any commas and dots.\r\n\t\t\t\tword = word.replace(\".\", \"\");\r\n\t\t\t\tword = word.replace(\",\", \"\");\r\n\t\t\t\t\r\n\t\t\t\tif(dictionary.containsKey(word)) {\r\n\t\t\t\t\tInteger val = dictionary.get(word);\r\n\t\t\t\t\tdictionary.put(word, val + 1);\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t\tdictionary.put(word, 1);\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\t\/\/ Printing all words stored in the map.\r\n\t\tSet<Entry> entries = dictionary.entrySet();\r\n\t\tIterator<Entry> iter = entries.iterator();\r\n\t\t\r\n\t\twhile(iter.hasNext()) {\r\n\t\t\tEntry entry = iter.next();\r\n\t\t\tSystem.out.println(entry.getKey() + \": \" + entry.getValue());\r\n\t\t}\r\n\t\t\r\n\t\treader.close();\r\n\t}\r\n}\r\n\r\n\r\nA sample execution is shown below:\r\nThis: 1\r\nis: 2\r\na: 1\r\nsample: 1\r\ntext: 1\r\nthat: 1\r\nused: 1\r\nto: 2\r\npresent: 1\r\nthe: 2\r\npower: 2\r\nof: 2\r\nJava: 2\r\nMaps: 1\r\nnamely: 1\r\nHashMaps: 1\r\nHashtables: 1\r\nand: 1\r\nLinkedHashMaps: 1\r\nAlso: 1\r\nhello: 1\r\neverybody: 1\r\nfrom: 1\r\nCode: 1\r\nGeeks: 1\r\n\r\nNotice that the usage of a LinkedHashMap enables us to print the stored keys in the way in which the words were read and stored in the map.\r\n\r\nDownload the Eclipse Project\r\nThe Eclipse project of this example: CountWords.zip\r\n\r\n \r\nThis was a tutorial about dictionaries in Java.","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/java-basics\/data-types\/java-dictionary-example\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Sotirios-Efstathios Maneas","url":"https:\/\/examples.javacodegeeks.com\/author\/stathis-maneas\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:author | https://www.facebook.com/stathis.maneas |
| article:published_time | 2014-02-07T20:19:52+00:00 |
| article:modified_time | 2014-02-07T20:30:07+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Sotirios-Efstathios Maneas |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Sotirios-Efstathios Maneas |
| twitter:label2 | Est. reading time |
| twitter:data2 | 4 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1