Title: Java Map Example - Examples Java Code Geeks - 2026
Open Graph Title: Java Map Example - Examples Java Code Geeks - 2026
Description: A Map is an interface that maps keys to values. The keys are unique and thus, no duplicate keys are allowed.
Open Graph Description: A Map is an interface that maps keys to values. The keys are unique and thus, no duplicate keys are allowed.
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/java-map-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/java-map-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/java-map-example/"},"author":{"name":"Sotirios-Efstathios Maneas","@id":"https://examples.javacodegeeks.com/#/schema/person/03673b064b32807176478bd144079be1"},"headline":"Java Map Example","datePublished":"2014-02-06T12:56:47+00:00","dateModified":"2022-07-12T17:27:09+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/java-map-example/"},"wordCount":685,"commentCount":2,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/java-map-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","keywords":["ConcurrentHashMap","featured","hashmap","hashtable","treemap"],"articleSection":["Java Basics"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/java-map-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/java-map-example/","url":"https://examples.javacodegeeks.com/java-map-example/","name":"Java Map Example - Examples Java Code Geeks - 2026","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/java-map-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/java-map-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2014-02-06T12:56:47+00:00","dateModified":"2022-07-12T17:27:09+00:00","description":"A Map is an interface that maps keys to values. The keys are unique and thus, no duplicate keys are allowed.","breadcrumb":{"@id":"https://examples.javacodegeeks.com/java-map-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/java-map-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/java-map-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-map-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":"Java Map 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\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2014-02-06T14:56:47+02:00","datePublished":"2014-02-06T14:56:47+02:00","dateModified":"2022-07-12T20:27:09+03:00","headline":"Java Map Example","name":"Java Map Example","keywords":"ConcurrentHashMap,featured,hashmap,hashtable,treemap","url":"https:\/\/examples.javacodegeeks.com\/java-map-example\/","description":"In this post, we feature a comprehensive Java Map Example. We will discuss about Maps in Java. A Map is an interface that maps keys to values. The keys are unique and thus, no duplicate keys are allow","copyrightYear":"2014","articleSection":"Java Basics","articleBody":"\nIn this post, we feature a comprehensive Java Map Example. We will discuss about Maps in Java. \n\n\n\nA Map is an interface that maps keys to values. The keys are unique and thus, no duplicate keys are allowed. 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. In addition, the order of the map is defined as the order in which, the elements of a map are returned during iteration.\n\n\n\nYou can also check the Hashmap Java Example in the following video:\n\n\n\nHashMap Java Example - Video\n\n\n\nThe Map interface is implemented by different Java classes, such as HashMap, HashTable, and TreeMap. Each class provides different functionality and can be either synchronized or not. Also, some implementations prohibit null keys and values, and some have restrictions on the types of their keys.\n\n\n\nFigure 1 Map\n\n\n\n1. Basic Methods\n\n\n\nA map has the form Map <K, V> where:\n\n\n\nK: specifies the type of keys maintained in this map.V: defines the type of mapped values.\n\n\n\nFurthermore, the Map interface provides a set of methods that must be implemented. In this section, we will discuss the most famous methods:\n\n\n\nclear: Removes all the elements from the map.containsKey: Returns true if the map contains the requested key.containsValue: Returns true if the map contains the requested value.equals: Compares an Object with the map for equality.get: Retrieve the value of the requested key.entrySet: Returns a Set view of the mappings contained in this map.keySet: Returns a Set that contains all keys of the map.put: Adds the requested key-value pair in the map.remove: Removes the requested key and its value from the map, if the key exists.size: Returns the number of key-value pairs currently in the map.\n\n\n\n2. Examples of Map\n\n\n\nThere are many classes that implement the Java Map interface. In this chapter, we will present the most common and used.\n\n\n\n2.1 HashMap\n\n\n\nThe most common class that implements the Map interface is the Java HashMap. A HashMap is a hash table-based implementation of the Map interface. It permits null keys and values. Also, this class does not maintain any order among its elements and especially, it does not guarantee that the order will remain constant over time. Finally, a HashMap contains 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.\n\n\n\nA simple example that uses a HashMap is shown below:\n\n\n\nHashMapExample.java:\n\n\n\npackage jcg.zheng.demo.data;\n\nimport java.util.HashMap;\nimport java.util.Map;\n\npublic class HashMapExample {\n\n\tpublic static void main(String[] args) {\n\t\tMap<String, Integer> vehicles = new HashMap<>();\n\n\t\t\/\/ Add some vehicles.\n\t\tvehicles.put(\"BMW\", 5);\n\t\tvehicles.put(\"Mercedes\", 3);\n\t\tvehicles.put(\"Audi\", 4);\n\t\tvehicles.put(\"Ford\", 10);\n\n\t\tSystem.out.println(\"Total vehicles: \" + vehicles.size());\n\n\t\t\/\/ Iterate over all vehicles, using the keySet method.\n\t\tfor (String key : vehicles.keySet())\n\t\t\tSystem.out.println(key + \" - \" + vehicles.get(key));\n\t\tSystem.out.println();\n\n\t\tString searchKey = \"Audi\";\n\t\tif (vehicles.containsKey(searchKey))\n\t\t\tSystem.out.println(\"Found total \" + vehicles.get(searchKey) + \" \" + searchKey + \" cars!\\n\");\n\n\t\t\/\/ Clear all values.\n\t\tvehicles.clear();\n\n\t\t\/\/ Equals to zero.\n\t\tSystem.out.println(\"After clear operation, size: \" + vehicles.size());\n\t}\n}\n\n\n\nA sample execution is shown below:\n\n\n\nTotal vehicles: 4\nAudi - 4\nFord - 10\nBMW - 5\nMercedes - 3\n\nFound total 4 Audi cars!\n\nAfter clear operation, size: 0\n\n\n\n2.2 HashTable\n\n\n\nThe HashTable class implements a hash table and maps keys to values. However, neither the key nor the value can be null. This class contains two fundamental parameters: initial capacity and performance, with the same definitions as the HashMap class.\n\n\n\nA simple example that uses a HashTable is shown below:\n\n\n\nHashTableExample.java:\n\n\n\npackage jcg.zheng.demo.data;\n\nimport java.util.Hashtable;\nimport java.util.Map;\n\npublic class HashTableExample {\n\tpublic static void main(String[] args) {\n\t\tMap<String, Integer> vehicles = new Hashtable<>();\n\n\t\t\/\/ Add some vehicles.\n\t\tvehicles.put(\"BMW\", 5);\n\t\tvehicles.put(\"Mercedes\", 3);\n\t\tvehicles.put(\"Audi\", 4);\n\t\tvehicles.put(\"Ford\", 10);\n\n\t\tSystem.out.println(\"Total vehicles: \" + vehicles.size());\n\n\t\t\/\/ Iterate over all vehicles, using the keySet method.\n\n\t\tfor (String key : vehicles.keySet())\n\t\t\tSystem.out.println(key + \" - \" + vehicles.get(key));\n\t\tSystem.out.println();\n\n\t\tString searchKey = \"Audi\";\n\t\tif (vehicles.containsKey(searchKey))\n\t\t\tSystem.out.println(\"Found total \" + vehicles.get(searchKey) + \" \" + searchKey + \" cars!\\n\");\n\n\t\t\/\/ Clear all values.\n\t\tvehicles.clear();\n\n\t\t\/\/ Equals to zero.\n\t\tSystem.out.println(\"After clear operation, size: \" + vehicles.size());\n\t\t\/\/ The next statements throw a NullPointerException, if uncommented.\n\t\t\/\/ vehicles.put(\"Nissan\", null);\n\t\t\/\/ vehicles.put(null, 6);\n\t}\n}\n\n\n\n\nA sample execution is shown below:\n\n\n\nTotal vehicles: 4\nAudi - 4\nFord - 10\nBMW - 5\nMercedes - 3\n\nFound total 4 Audi cars!\n\nAfter clear operation, size: 0\n\n\n\n2.3 TreeMap\n\n\n\nThe TreeMap is a Red-Black tree implementation that is sorted according to the natural ordering of its keys, or by a Comparator provided at the creation time. Also, this class maintains order on its elements. Finally, this class is not synchronized and thus, if an application uses multiple threads, the map must be synchronized externally.\n\n\n\nA simple example that uses a TreeMap is shown below:\n\n\n\nTreeMapExample.java:\n\n\n\npackage jcg.zheng.demo.data;\n\nimport java.util.Map;\nimport java.util.TreeMap;\n\npublic class TreeMapExample {\n\tpublic static void main(String[] args) {\n\t\tMap<String, Integer> vehicles = new TreeMap<>();\n\n\t\t\/\/ Add some vehicles.\n\t\tvehicles.put(\"BMW\", 5);\n\t\tvehicles.put(\"Mercedes\", 3);\n\t\tvehicles.put(\"Audi\", 4);\n\t\tvehicles.put(\"Ford\", 10);\n\n\t\tSystem.out.println(\"Total vehicles: \" + vehicles.size());\n\n\t\t\/\/ Iterate over all vehicles, using the keySet method.\n\t\tfor (String key : vehicles.keySet())\n\t\t\tSystem.out.println(key + \" - \" + vehicles.get(key));\n\t\tSystem.out.println();\n\n\t\tSystem.out.println(\"Highest key: \" + ((TreeMap) vehicles).lastKey());\n\t\tSystem.out.println(\"Lowest key: \" + ((TreeMap) vehicles).firstKey());\n\n\t\tSystem.out.println(\"\\nPrinting all values:\");\n\n\t\tfor (Integer val : vehicles.values())\n\t\t\tSystem.out.println(val);\n\t\tSystem.out.println();\n\n\t\t\/\/ Clear all values.\n\t\tvehicles.clear();\n\n\t\t\/\/ Equals to zero.\n\t\tSystem.out.println(\"After clear operation, size: \" + vehicles.size());\n\t}\n}\n\n\n\n\nA sample execution is shown below:\n\n\n\nTotal vehicles: 4\nAudi - 4\nBMW - 5\nFord - 10\nMercedes - 3\n\nHighest key: Mercedes\nLowest key: Audi\n\nPrinting all values:\n4\n5\n10\n3\n\nAfter clear operation, size: 0\n\n\n\nAs you can observe, the elements of the map are printed in a strict lexicographic order, which does not appear in the previous examples of HashMap and HashTable.\n\n\n\n2.4 ConcurrentHashMap\n\n\n\nThe class is a hash table that supports the full concurrency of retrievals. Thus, this structure is safe to use in case of multiple threads. Finally, this class allows neither keys nor values to be null.\n\n\n\nA simple example that uses a ConcurrentHashMap is shown below:\n\n\n\nConcurrentHashMapExample.java:\n\n\n\npackage jcg.zheng.demo.data;\n\nimport java.util.Enumeration;\nimport java.util.Map;\nimport java.util.concurrent.ConcurrentHashMap;\n\npublic class ConcurrentHashMapExample {\n\n\tpublic static void main(String[] args) {\n\t\tMap<String, Integer> vehicles = new ConcurrentHashMap<>();\n\n\t\t\/\/ Add some vehicles.\n\t\tvehicles.put(\"BMW\", 5);\n\t\tvehicles.put(\"Mercedes\", 3);\n\t\tvehicles.put(\"Audi\", 4);\n\t\tvehicles.put(\"Ford\", 10);\n\n\t\tSystem.out.println(\"Total vehicles: \" + vehicles.size());\n\n\t\t\/\/ Iterate over all vehicles, using the keySet method.\n\t\tfor (String key : vehicles.keySet())\n\t\t\tSystem.out.println(key + \" - \" + vehicles.get(key));\n\n\t\tSystem.out.println();\n\n\t\tString searchKey = \"Audi\";\n\t\tif (vehicles.containsKey(searchKey))\n\t\t\tSystem.out.println(\"Found total \" + vehicles.get(searchKey) + \" \" + searchKey + \" cars!\\n\");\n\n\t\tEnumeration<Integer> elems = ((ConcurrentHashMap) vehicles).elements();\n\t\twhile (elems.hasMoreElements())\n\t\t\tSystem.out.println(elems.nextElement());\n\t\tSystem.out.println();\n\n\t\tInteger val = (Integer) vehicles.putIfAbsent(\"Audi\", 9);\n\t\tif (val != null)\n\t\t\tSystem.out.println(\"Audi was found in the map and its value was updated!\");\n\n\t\tval = (Integer) vehicles.putIfAbsent(\"Nissan\", 9);\n\t\tif (val == null)\n\t\t\tSystem.out.println(\"Nissan wasn't found in map, thus a new pair was created!\");\n\t\tSystem.out.println();\n\n\t\t\/\/ The next statements throw a NullPointerException, if uncommented.\n\t\t\/\/ vehicles.put(\"Nissan\", null);\n\t\t\/\/ vehicles.put(null, 6);\n\n\t\t\/\/ Clear all values.\n\t\tvehicles.clear();\n\n\t\t\/\/ Equals to zero.\n\t\tSystem.out.println(\"After clear operation, size: \" + vehicles.size());\n\t}\n}\n\n\n\nA sample execution is shown below:\n\n\n\nTotal vehicles: 4\nBMW - 5\nMercedes - 3\nAudi - 4\nFord - 10\n\nFound total 4 Audi cars!\n\n5\n3\n4\n10\n\nAudi was found in the map and its value was updated!\nNissan wasn't found in map, thus a new pair was created!\n\nAfter clear operation, size: 0\n\n\n\n3. More articles\n\n\n\nHashmap Java ExampleHashset Java Example\n\n\n\n4. Download The Eclipse Project\n\n\n\nThis was a tutorial about Maps in Java.\n\n\n\nDownloadYou can download the full source code of this example here: Java Map Example\n\n\n\nLast updated on Feb. 24th, 2022\n","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-map-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-06T12:56:47+00:00 |
| article:modified_time | 2022-07-12T17:27:09+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 | 6 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