{"id":13334,"date":"2014-08-22T22:15:37","date_gmt":"2014-08-22T19:15:37","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=13334"},"modified":"2014-08-22T22:27:40","modified_gmt":"2014-08-22T19:27:40","slug":"java-sorted-map-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/","title":{"rendered":"Java Sorted Map Example"},"content":{"rendered":"<p>In this example we shall show you how to make use of Java Sorted Map. A\u00a0<code><a title=\"Sorted Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">SortedMap<\/a><\/code>\u00a0is a\u00a0<code><a title=\"Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Map.html\" target=\"_blank\">Map<\/a><\/code>\u00a0that sort its entries in ascending order according to the keys&#8217; natural ordering, or according to a\u00a0<code><a title=\"Comparator\" href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Comparator.html\" target=\"_blank\">Comparator<\/a><\/code>\u00a0provided at the time of the\u00a0<code><a title=\"Sorted Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">SortedMap<\/a><\/code>\u00a0creation. All keys inserted into a\u00a0<code><a title=\"Sorted Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">SortedMap<\/a><\/code>\u00a0must implement the\u00a0<code><a title=\"Comparable\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Comparable.html\" target=\"_blank\">Comparable<\/a><\/code>\u00a0interface (or be accepted by the specified\u00a0<code><a title=\"Comparator\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Comparator.html\" target=\"_blank\">Comparator<\/a><\/code>). Furthermore, all such elements must be mutually comparable\u00a0<i>(i.e, Mutually Comparable simply means that two objects accept each other as the argument to their\u00a0<\/i><code><a title=\"compareTo\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Comparable.html#compareTo-T-\" target=\"_blank\"><i>compareTo<\/i><\/a><\/code><i>\u00a0method)<\/i>, If you try to sort keys which do not implement\u00a0<code><a title=\"Comparable\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Comparable.html\" target=\"_blank\">Comparable<\/a><\/code>\u00a0or not has a specific\u00a0<code><a title=\"Comparator\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Comparator.html\" target=\"_blank\">Comparator<\/a><\/code>, a\u00a0<code><a title=\"ClassCastException\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/ClassCastException.html\" target=\"_blank\">ClassCastException<\/a><\/code>\u00a0will be thrown.<\/p>\n<div class=\"tip\">\n<p><strong>Tip 1<\/strong><\/p>\n<p><code><a title=\"int compareTo(Object o)\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Comparable.html#compareTo-T-\" target=\"_blank\">java.lang.Comparable: int compareTo(Object o):<\/a><\/code><\/p>\n<p>This method compares this object with o object. Returned int value has the following meanings.<\/p>\n<ul>\n<li><code>positive<\/code> \u2013 this object is greater than o<\/li>\n<li><code>zero<\/code> \u2013 this object equals to o<\/li>\n<li><code>negative<\/code> \u2013 this object is less than o<\/li>\n<\/ul>\n<p>Also, we can use our own <code><a title=\"Comparator\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Comparator.html\" target=\"_blank\">Comparator<\/a><\/code>. If you need to know more about the <code><a title=\"Comparable\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Comparable.html\" target=\"_blank\">Comparable<\/a><\/code>\u00a0and <code><a title=\"Comparator\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Comparator.html\" target=\"_blank\">Comparator<\/a><\/code>, Take a look on <a title=\"Java Comparable and Comparator Example to sort Objects\" href=\"http:\/\/examples.javacodegeeks.com\/core-java\/util\/comparator\/java-comparable-and-comparator-example-to-sort-objects\" target=\"_blank\">Java Comparable and Comparator Example to sort Objects<\/a> by <a title=\"Byron Kiourtzoglou\" href=\"http:\/\/examples.javacodegeeks.com\/author\/byron-kiourtzoglou\" target=\"_blank\">Byron Kiourtzoglou<\/a>.<\/p>\n<\/div>\n<div class=\"tip\">\n<p><strong>Tip 2<\/strong><\/p>\n<p>All <code><a title=\"Sorted Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">SortedMap<\/a><\/code>\u00a0implementation classes should provide four &#8220;standard&#8221; constructors as the following:<\/p>\n<ul>\n<li>A void (no arguments) constructor, which creates an empty <code><a title=\"Sorted Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">SortedMap<\/a><\/code>\u00a0sorted according to the natural ordering\u00a0of its keys.\n<pre class=\"brush:java\">SortedMap sortedMap= new TreeMap();<\/pre>\n<\/li>\n<li>A constructor with a single argument of type <code><a title=\"Comparator\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Comparator.html\" target=\"_blank\">Comparator<\/a><\/code>, which creates an empty <code><a title=\"Sorted Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">SortedMap<\/a><\/code>\u00a0sorted according to the specified <code><a title=\"Comparator\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Comparator.html\" target=\"_blank\">Comparator<\/a><\/code>.\n<pre class=\"brush:java\">Comparator comparator = new MyComparator();\r\nSortedMap sortedMap = new TreeMap(comparator);<\/pre>\n<\/li>\n<li>A constructor with a single argument of type <code><a title=\"Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Map.html\" target=\"_blank\">Map<\/a><\/code>, which creates a new <code><a title=\"Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Map.html\" target=\"_blank\">Map<\/a><\/code>\u00a0with the same key-value mappings as its argument, sorted according to the keys&#8217; natural ordering.\n<pre class=\"brush:java\">Map map = new HashMap();\r\nSortedMap sortedMap = new TreeMap(map);<\/pre>\n<\/li>\n<li>A constructor with a single argument of type <code><a title=\"Sorted Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">SortedMap<\/a><\/code>, which creates a new <code><a title=\"Sorted Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">SortedMap<\/a><\/code>\u00a0with the same key-value mappings and the same ordering as the input <code><a title=\"Sorted Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">SortedMap<\/a><\/code>.\n<pre class=\"brush:java\">SortedMap sortedMap= new TreeMap();\r\nSortedMap newSortedMap = new TreeMap(sortedMap);<\/pre>\n<\/li>\n<\/ul>\n<\/div>\n<h2>1. SortedMap Operations:<\/h2>\n<p>The <code><a title=\"SortedMap\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">SortedMap<\/a><\/code> interface provides operations for normal Map operations and for the following:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<ul>\n<li><b>Range view<\/b> \u2014 performs arbitrary range operations on the\u00a0<code><a title=\"SortedMap\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">SortedMap<\/a><\/code>\n<ol>\n<li><code><b><a title=\"subMap\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html#subMap-K-K-\" target=\"_blank\">subMap<\/a>(<a title=\"K\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">K<\/a>\u00a0fromKey,\u00a0<a title=\"K\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">K<\/a>\u00a0toKey)<\/b><\/code>: Returns a view of the portion of this <code><a title=\"Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Map.html\" target=\"_blank\">Map<\/a><\/code>\u00a0whose keys range from fromKey, inclusive, to toKey, exclusive.<\/li>\n<li><code><b><a title=\"headMap\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html#headMap-K-\" target=\"_blank\">headMap<\/a>(<a title=\"K\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">K<\/a>\u00a0toKey)<\/b><\/code>: Returns a view of the portion of this <code><a title=\"Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Map.html\" target=\"_blank\">Map<\/a><\/code>\u00a0whose keys are strictly less than toKey.<\/li>\n<li><code><b><a title=\"tailMap\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html#tailMap-K-\" target=\"_blank\">tailMap<\/a>(<a title=\"K\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">K<\/a>\u00a0fromKey)<\/b><\/code>: Returns a view of the portion of this <code><a title=\"Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Map.html\" target=\"_blank\">Map<\/a><\/code>\u00a0whose keys are greater than or equal to fromKey.<\/li>\n<\/ol>\n<\/li>\n<li><b>Endpoints <\/b>\u2014 returns the first or the last key in the\u00a0<code><a title=\"SortedMap\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">SortedMap<\/a><\/code>\n<ol>\n<li><code><b><a title=\"firstKey\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html#firstKey--\" target=\"_blank\">firstKey<\/a>()<\/b><\/code>: Returns the first (lowest) key currently in this <code><a title=\"Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Map.html\" target=\"_blank\">Map<\/a><\/code>.<\/li>\n<li><code><b><a title=\"lastKey\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html#lastKey--\" target=\"_blank\">lastKey<\/a>()<\/b><\/code>: Returns the last (highest) key currently in this <code><a title=\"Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Map.html\" target=\"_blank\">Map<\/a><\/code>.<\/li>\n<\/ol>\n<\/li>\n<li><b>Comparator access<\/b> \u2014 returns the <code><a title=\"Comparator\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Comparator.html\" target=\"_blank\">Comparator<\/a><\/code>, if any, used to sort the map\n<ol>\n<li><code> <b><a title=\"comparator\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html#comparator--\" target=\"_blank\">comparator<\/a>()<\/b><\/code>: Returns the <code><a title=\"Comparator\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Comparator.html\" target=\"_blank\">Comparator<\/a><\/code>\u00a0used to order the keys in this <code><a title=\"Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Map.html\" target=\"_blank\">Map<\/a><\/code>, or null if this <code><a title=\"Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Map.html\" target=\"_blank\">Map<\/a><\/code>\u00a0uses the natural ordering\u00a0of its keys.<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h2>2. Example:<\/h2>\n<h3>2.1. SortMapExample.java<\/h3>\n<pre class=\"brush:java\">package com.jcg.util.map;\r\n\r\nimport java.util.Comparator;\r\nimport java.util.HashMap;\r\nimport java.util.Map;\r\nimport java.util.TreeMap;\r\n\r\n\/**\r\n * @author ashraf\r\n * \r\n *\/\r\npublic class SortMapExample {\r\n\r\n\t\/**\r\n\t * The main method.\r\n\t *\r\n\t * @param args the arguments\r\n\t *\/\r\n\tpublic static void main(String[] args) {\r\n\t\t\/\/creating unsorted map of employee id as a key and employee name as a value\r\n\t\tMap unsortMap = new HashMap();\r\n\t\tunsortMap.put(10, \"Ashraf\");\r\n\t\tunsortMap.put(5, \"Sara\");\r\n\t\tunsortMap.put(6, \"Mohamed\");\r\n\t\tunsortMap.put(20, \"Esraa\");\r\n\t\tunsortMap.put(1, \"Bahaa\");\r\n\t\tunsortMap.put(7, \"Dalia\");\r\n\t\tunsortMap.put(8, \"Amira\");\r\n\t\tunsortMap.put(99, \"Ahmed\");\r\n\t\tunsortMap.put(50, \"Sama\");\r\n\t\tunsortMap.put(2, \"Nada\");\r\n\t\tunsortMap.put(9, \"Osama\");\r\n\r\n\t\tSystem.out.println(\"Unsort Map......\");\r\n\t\tprintMap(unsortMap);\r\n\r\n\t\t\/\/ Using the default natural ordering of sorted map Integer key which implement Comparable interface\r\n\t\tSystem.out.println(\"\\nSorted Map in ascending order......\");\r\n\t\tMap ascSortedMap = new TreeMap();\r\n\t\tascSortedMap.putAll(unsortMap);\r\n\t\tprintMap(ascSortedMap);\r\n\r\n\t\t\/\/ Forcing the descending order by creating our own comparator then passing it to the sorted map at creation time\r\n\t\tSystem.out.println(\"\\nSorted Map in descending order......\");\r\n\t\tMap desSortedMap = new TreeMap(\r\n\t\t\t\tnew Comparator() {\r\n\r\n\t\t\t\t\t@Override\r\n\t\t\t\t\tpublic int compare(Integer o1, Integer o2) {\r\n\t\t\t\t\t\treturn o2.compareTo(o1);\r\n\t\t\t\t\t}\r\n\r\n\t\t\t\t});\r\n\t\tdesSortedMap.putAll(unsortMap);\r\n\t\tprintMap(desSortedMap);\r\n\r\n\t}\r\n\r\n\t\/**\r\n\t * Prints the map.\r\n\t *\r\n\t * @param map the map\r\n\t *\/\r\n\tpublic static void printMap(Map map) {\r\n\t\tfor (Map.Entry entry : map.entrySet()) {\r\n\t\t\tSystem.out.println(\"Key : \" + entry.getKey() + \" Value : \"\r\n\t\t\t\t\t+ entry.getValue());\r\n\t\t}\r\n\t}\r\n\r\n}\r\n<\/pre>\n<h3>2.2. Explanation:<\/h3>\n<p>Let\u2019s suppose that we want to sort a <code><a title=\"Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Map.html\" target=\"_blank\">Map<\/a><\/code>\u00a0which contains a group of employees according to their ids\u00a0where we use the employee id as a key and the employee name as a value. After\u00a0we create a <code><a title=\"Sorted Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">SortedMap<\/a><\/code>\u00a0using this <code><a title=\"Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Map.html\" target=\"_blank\">Map<\/a><\/code>\u00a0and\u00a0the key of this <code><a title=\"Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Map.html\" target=\"_blank\">Map<\/a><\/code>\u00a0is an <code><a title=\"Integer\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Integer.html\" target=\"_blank\">Integer<\/a><\/code> type which implements the\u00a0<code><a title=\"Comparable\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/lang\/Comparable.html\" target=\"_blank\">Comparable<\/a><\/code>\u00a0interface, the\u00a0keys are ordered in their natural ordering. Also, we can apply the descending order by creating our own <code><a title=\"Comparator\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/Comparator.html\" target=\"_blank\">Comparator<\/a><\/code>\u00a0then passing it to the <code><a title=\"Sorted Map\" href=\"http:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/SortedMap.html\" target=\"_blank\">SortedMap<\/a><\/code>\u00a0at creation time.<\/p>\n<h3>2.3. Output:<\/h3>\n<pre class=\"brush:xml\">Unsort Map......\r\nKey : 50 Value : Sama\r\nKey : 1 Value : Bahaa\r\nKey : 2 Value : Nada\r\nKey : 99 Value : Ahmed\r\nKey : 20 Value : Esraa\r\nKey : 5 Value : Sara\r\nKey : 6 Value : Mohamed\r\nKey : 7 Value : Dalia\r\nKey : 8 Value : Amira\r\nKey : 9 Value : Osama\r\nKey : 10 Value : Ashraf\r\n\r\nSorted Map in ascending order......\r\nKey : 1 Value : Bahaa\r\nKey : 2 Value : Nada\r\nKey : 5 Value : Sara\r\nKey : 6 Value : Mohamed\r\nKey : 7 Value : Dalia\r\nKey : 8 Value : Amira\r\nKey : 9 Value : Osama\r\nKey : 10 Value : Ashraf\r\nKey : 20 Value : Esraa\r\nKey : 50 Value : Sama\r\nKey : 99 Value : Ahmed\r\n\r\nSorted Map in descending order......\r\nKey : 99 Value : Ahmed\r\nKey : 50 Value : Sama\r\nKey : 20 Value : Esraa\r\nKey : 10 Value : Ashraf\r\nKey : 9 Value : Osama\r\nKey : 8 Value : Amira\r\nKey : 7 Value : Dalia\r\nKey : 6 Value : Mohamed\r\nKey : 5 Value : Sara\r\nKey : 2 Value : Nada\r\nKey : 1 Value : Bahaa\r\n<\/pre>\n<h2>3. Download the Source Code of this example:<\/h2>\n<p>This was an example of how to use Java Sorted Map.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a title=\"Java Sorted Map Example Code\" href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/08\/java-sorted-map-example-code.zip\" target=\"_blank\"><strong>Java Sorted Map Example Code<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this example we shall show you how to make use of Java Sorted Map. A\u00a0SortedMap\u00a0is a\u00a0Map\u00a0that sort its entries in ascending order according to the keys&#8217; natural ordering, or according to a\u00a0Comparator\u00a0provided at the time of the\u00a0SortedMap\u00a0creation. All keys inserted into a\u00a0SortedMap\u00a0must implement the\u00a0Comparable\u00a0interface (or be accepted by the specified\u00a0Comparator). Furthermore, all such elements &hellip;<\/p>\n","protected":false},"author":24,"featured_media":1204,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[169],"tags":[645,655],"class_list":["post-13334","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-treemap","tag-java-collections","tag-sorted-map"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Sorted Map Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In this example we shall show you how to make use of Java Sorted Map. A\u00a0SortedMap\u00a0is a\u00a0Map\u00a0that sort its entries in ascending order according to the keys&#039;\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Sorted Map Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In this example we shall show you how to make use of Java Sorted Map. A\u00a0SortedMap\u00a0is a\u00a0Map\u00a0that sort its entries in ascending order according to the keys&#039;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/ashraf.sar7an\" \/>\n<meta property=\"article:published_time\" content=\"2014-08-22T19:15:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2014-08-22T19:27:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Ashraf Sarhan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@http:\/\/twitter.com\/ashraf_sarhan\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ashraf Sarhan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/\"},\"author\":{\"name\":\"Ashraf Sarhan\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/553eb8f56e9ffb76e6bcc85d6157fc91\"},\"headline\":\"Java Sorted Map Example\",\"datePublished\":\"2014-08-22T19:15:37+00:00\",\"dateModified\":\"2014-08-22T19:27:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/\"},\"wordCount\":515,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"keywords\":[\"Java Collections\",\"Sorted Map\"],\"articleSection\":[\"TreeMap\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/\",\"name\":\"Java Sorted Map Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg\",\"datePublished\":\"2014-08-22T19:15:37+00:00\",\"dateModified\":\"2014-08-22T19:27:40+00:00\",\"description\":\"In this example we shall show you how to make use of Java Sorted Map. A\u00a0SortedMap\u00a0is a\u00a0Map\u00a0that sort its entries in ascending order according to the keys'\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-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-development\/core-java\/util\/treemap\/java-sorted-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\":\"Core Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"util\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/util\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"TreeMap\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/util\/treemap\/\"},{\"@type\":\"ListItem\",\"position\":6,\"name\":\"Java Sorted 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\/553eb8f56e9ffb76e6bcc85d6157fc91\",\"name\":\"Ashraf Sarhan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2024\/04\/cropped-ashraf_sarhan_photo-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2024\/04\/cropped-ashraf_sarhan_photo-96x96.jpg\",\"caption\":\"Ashraf Sarhan\"},\"description\":\"Ashraf Sarhan is a passionate software engineer, an open source enthusiast, has a Bsc. degree in Computer and Information Systems from Alexandria University. He is experienced in building large, scalable and distributed enterprise applications\/service in multiple domains. He also has a keen interest in JavaEE, SOA, Agile and Big Data technologies.\",\"sameAs\":[\"http:\/\/www.javacodegeeks.com\",\"https:\/\/www.facebook.com\/ashraf.sar7an\",\"https:\/\/eg.linkedin.com\/in\/ashrafsarhan\",\"https:\/\/x.com\/http:\/\/twitter.com\/ashraf_sarhan\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/ashraf-sarhan\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Sorted Map Example - Java Code Geeks","description":"In this example we shall show you how to make use of Java Sorted Map. A\u00a0SortedMap\u00a0is a\u00a0Map\u00a0that sort its entries in ascending order according to the keys'","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/","og_locale":"en_US","og_type":"article","og_title":"Java Sorted Map Example - Java Code Geeks","og_description":"In this example we shall show you how to make use of Java Sorted Map. A\u00a0SortedMap\u00a0is a\u00a0Map\u00a0that sort its entries in ascending order according to the keys'","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_author":"https:\/\/www.facebook.com\/ashraf.sar7an","article_published_time":"2014-08-22T19:15:37+00:00","article_modified_time":"2014-08-22T19:27:40+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","type":"image\/jpeg"}],"author":"Ashraf Sarhan","twitter_card":"summary_large_image","twitter_creator":"@http:\/\/twitter.com\/ashraf_sarhan","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Ashraf Sarhan","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/"},"author":{"name":"Ashraf Sarhan","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/553eb8f56e9ffb76e6bcc85d6157fc91"},"headline":"Java Sorted Map Example","datePublished":"2014-08-22T19:15:37+00:00","dateModified":"2014-08-22T19:27:40+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/"},"wordCount":515,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","keywords":["Java Collections","Sorted Map"],"articleSection":["TreeMap"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/","name":"Java Sorted Map Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","datePublished":"2014-08-22T19:15:37+00:00","dateModified":"2014-08-22T19:27:40+00:00","description":"In this example we shall show you how to make use of Java Sorted Map. A\u00a0SortedMap\u00a0is a\u00a0Map\u00a0that sort its entries in ascending order according to the keys'","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-map-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/util\/treemap\/java-sorted-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-development\/core-java\/util\/treemap\/java-sorted-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":"Core Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"},{"@type":"ListItem","position":4,"name":"util","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/util\/"},{"@type":"ListItem","position":5,"name":"TreeMap","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/util\/treemap\/"},{"@type":"ListItem","position":6,"name":"Java Sorted 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\/553eb8f56e9ffb76e6bcc85d6157fc91","name":"Ashraf Sarhan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2024\/04\/cropped-ashraf_sarhan_photo-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2024\/04\/cropped-ashraf_sarhan_photo-96x96.jpg","caption":"Ashraf Sarhan"},"description":"Ashraf Sarhan is a passionate software engineer, an open source enthusiast, has a Bsc. degree in Computer and Information Systems from Alexandria University. He is experienced in building large, scalable and distributed enterprise applications\/service in multiple domains. He also has a keen interest in JavaEE, SOA, Agile and Big Data technologies.","sameAs":["http:\/\/www.javacodegeeks.com","https:\/\/www.facebook.com\/ashraf.sar7an","https:\/\/eg.linkedin.com\/in\/ashrafsarhan","https:\/\/x.com\/http:\/\/twitter.com\/ashraf_sarhan"],"url":"https:\/\/examples.javacodegeeks.com\/author\/ashraf-sarhan\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/13334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/24"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=13334"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/13334\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1204"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=13334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=13334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=13334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}