{"id":1300,"date":"2016-09-06T06:49:06","date_gmt":"2016-09-06T06:49:06","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1300"},"modified":"2024-12-14T03:36:34","modified_gmt":"2024-12-14T03:36:34","slug":"write-a-java-program-using-hashmap-to-store-name-and-age-pairs-and-print-the-details","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-java-program-using-hashmap-to-store-name-and-age-pairs-and-print-the-details\/","title":{"rendered":"Write a Java program using HashMap to store name and age pairs"},"content":{"rendered":"<h2>Write a Java program using HashMap to store name and age pairs<\/h2>\n<p>In this tutorial, we will learn to use HashMap to store name and age pairs. HashMap maps keys to values. It implements the Map interface. HashMap is not synchronized and permits nulls. Furthermore, it makes no guarantee as to the order of the map.<\/p>\n<p>In the below sample program, we will store the name\/age pair and later display the data stored. One important thing to notice is that you can\u2019t have duplicates in the keys.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1272\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/HashMap-Class.png\" alt=\"HashMap Class\" width=\"816\" height=\"1056\" title=\"\"><\/p>\n<p>If multiple threads access a hash map concurrently, and if threads modify the map you need to synchronize externally. to avoid ConcurrentModificationException.<\/p>\n<h3><strong>Java program<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import java.util.HashMap;\r\nimport java.util.Scanner;\r\n\r\npublic class HashMapExample {\r\n    public static void main(String[] args) {\r\n\r\n        Scanner input = null;\r\n        String name;\r\n        int age ;\r\n        HashMap&lt;String, Integer&gt; hMap = new HashMap&lt;String, Integer&gt;();\r\n        try {\r\n            input=new Scanner(System.in);\r\n            System.out.println(\"Build HashMap with Details:\");\r\n            while (input.hasNext()) {\r\n                name = input.next();\r\n                age = input.nextInt();\r\n                hMap.put(name,age);\r\n            }\r\n            System.out.println(\"HashMap with Details:\");\r\n            hMap.forEach((Name,Age) -&gt; System.out.println(\"Name: \"+Name+\r\n\" Age:\"+ Age));\r\n        }\r\n        catch (Exception e) {\r\n            e.printStackTrace();\r\n        } finally\r\n        {\r\n            if(input!=null)\r\n            {input.close();}\r\n        }\r\n\r\n    }\r\n\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1273\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/HashMap-Example.png\" alt=\"HashMap Example\" width=\"1056\" height=\"816\" title=\"\"><\/h2>\n<h3><strong>Output of the program<\/strong><\/h3>\n<p>Build HashMap with Details:<br \/>\nAlbert<br \/>\n23<br \/>\nRegan<br \/>\n38<br \/>\nBrenda<br \/>\n29<br \/>\nJesse<br \/>\n31<br \/>\n^D<br \/>\nHashMap with Details:<br \/>\nName: Regan Age:38<br \/>\nName: Brenda Age:29<br \/>\nName: Jesse Age:31<br \/>\nName: Albert Age:23<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1274\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/HashMap-Sample-Java-Program.png\" alt=\"HashMap Sample Java Program\" width=\"1252\" height=\"576\" title=\"\"><\/p>\n<p>&nbsp;<\/p>\n<p>Java Tutorial on this website:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Write a Java program using HashMap to store name and age pairs In this tutorial, we will learn to use HashMap to store name and age pairs. HashMap maps keys to values. It implements the Map interface. HashMap is not synchronized and permits nulls. Furthermore, it makes no guarantee as to the order of the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1300","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1300"}],"version-history":[{"count":10,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1300\/revisions"}],"predecessor-version":[{"id":26342,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1300\/revisions\/26342"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}