{"id":1265,"date":"2021-01-14T15:12:09","date_gmt":"2021-01-14T15:12:09","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=1265"},"modified":"2021-01-14T15:33:29","modified_gmt":"2021-01-14T15:33:29","slug":"the-collections-framework","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/generic-programming\/the-collections-framework\/","title":{"rendered":"The Collections Framework"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_83 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a06ee190fe7a\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a06ee190fe7a\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/generic-programming\/the-collections-framework\/#VectorEnumeration_Example\" >Vector\/Enumeration Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/generic-programming\/the-collections-framework\/#ArrayListIterator_Example\" >ArrayList\/Iterator Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/generic-programming\/the-collections-framework\/#LinkedList_Example\" >LinkedList Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/generic-programming\/the-collections-framework\/#Stack_Example\" >Stack Example<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/generic-programming\/the-collections-framework\/#Changing_the_Type\" >Changing the Type<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/generic-programming\/the-collections-framework\/#Comparable_Example\" >Comparable Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/generic-programming\/the-collections-framework\/#Comparator_Example\" >Comparator Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/generic-programming\/the-collections-framework\/#HashSet_Example\" >HashSet Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/generic-programming\/the-collections-framework\/#TreeSet_Example\" >TreeSet Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/generic-programming\/the-collections-framework\/#TreeSet_with_Comparator\" >TreeSet with Comparator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/generic-programming\/the-collections-framework\/#HashMap_Example\" >HashMap Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/generic-programming\/the-collections-framework\/#TreeMap_Example\" >TreeMap Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/generic-programming\/the-collections-framework\/#TreeMap_with_Comparator\" >TreeMap with Comparator<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>Remember the <em>Collections Framework<\/em>?<\/p>\n\n\n\n<p>All of the Collections Framework classes and interfaces allow you to use generics (but they also allow you to choose not to use generics).&nbsp;<\/p>\n\n\n\n<p>As I think you&#8217;ll see, using generics is almost always the preferred approach.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"VectorEnumeration_Example\"><\/span>Vector\/Enumeration Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here&#8217;s the&nbsp;<strong>Vector\/Enumeration<\/strong>&nbsp;example from before, translated to using generics:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage examples;\n\nimport java.util.Enumeration;\nimport java.util.Vector;\n\npublic class VectorExample\n{\n  public static void main(String&#x5B;] args)\n  {\n    Vector&lt;String&gt; vector = new Vector&lt;String&gt;();\n    vector.add(&quot;Mary&quot;);\n    vector.add(&quot;Frank&quot;);\n    vector.add(&quot;Joe&quot;);\n    vector.add(&quot;Sylvia&quot;);\n    vector.add(&quot;Vanessa&quot;);\n    \n    for (int i = 0; i &lt; vector.size(); i++)\n    {\n      String name = vector.elementAt(i); \/\/ Note NO required cast\n      System.out.println(i + &quot;: &quot; + name);\n    }\n    \n    for (Enumeration&lt;String&gt; en = vector.elements(); en.hasMoreElements(); )\n    {\n      String name = en.nextElement(); \/\/ Note NO required cast\n      System.out.println(name);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p>which outputs the following (the same as the original):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>0: Mary\n1: Frank\n2: Joe\n3: Sylvia\n4: Vanessa\nMary\nFrank\nJoe\nSylvia\nVanessa<\/strong><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"ArrayListIterator_Example\"><\/span>ArrayList\/Iterator Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here is the&nbsp;<strong>ArrayList\/Iterator<\/strong>&nbsp;example, translated to using generics:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage examples;\n\nimport java.util.ArrayList;\nimport java.util.Iterator;\n\npublic class ArrayListExample\n{\n  public static void main(String&#x5B;] args)\n  {\n    ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();\n    list.add(&quot;Mary&quot;);\n    list.add(&quot;Frank&quot;);\n    list.add(&quot;Joe&quot;);\n    list.add(&quot;Sylvia&quot;);\n    list.add(&quot;Vanessa&quot;);\n\n    for (int i = 0; i &lt; list.size(); i++)\n    {\n      String name = list.get(i); \/\/ Note NO required cast\n      System.out.println(i + &quot;: &quot; + name);\n    }\n\n    for (Iterator&lt;String&gt; iter = list.iterator(); iter.hasNext(); )\n    {\n      String name = iter.next(); \/\/ Note NO required cast\n      System.out.println(name);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p>which produces the output (same as original):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>0: Mary\n1: Frank\n2: Joe\n3: Sylvia\n4: Vanessa\nMary\nFrank\nJoe\nSylvia\nVanessa<\/strong><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"LinkedList_Example\"><\/span>LinkedList Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here is the&nbsp;<strong>LinkedList<\/strong>&nbsp;example, translated to using generics:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage examples;\n\nimport java.util.LinkedList;\nimport java.util.List;\nimport java.util.ListIterator;\n\npublic class LinkedListExample\n{\n  public static void main(String&#x5B;] args)\n  {\n    List&lt;String&gt; list = new LinkedList&lt;String&gt;();\n    list.add(&quot;Mary&quot;);\n    list.add(&quot;Frank&quot;);\n    list.add(&quot;Joe&quot;);\n    list.add(&quot;Sylvia&quot;);\n    list.add(&quot;Vanessa&quot;);\n\n    for (int i = 0; i &lt; list.size(); i++)\n    {\n      String name = list.get(i); \/\/ Note NO required cast\n      System.out.println(i + &quot;: &quot; + name);\n    }\n\n    for (ListIterator&lt;String&gt; iter = list.listIterator(); iter.hasNext(); )\n    {\n      String name = iter.next(); \/\/ Note NO required cast\n      System.out.println(name);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p>This program produces the output (same as the original):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>0: Mary\n1: Frank\n2: Joe\n3: Sylvia\n4: Vanessa\nMary\nFrank\nJoe\nSylvia\nVanessa<\/strong><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Stack_Example\"><\/span>Stack Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here&#8217;s the&nbsp;<strong>Stack<\/strong>&nbsp;example, translated to using generics:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage examples;\n\nimport java.util.Stack;\n\npublic class StackExample\n{\n  public static void main(String&#x5B;] args)\n  {\n    Stack&lt;String&gt; stack = new Stack&lt;String&gt;();\n    stack.push(&quot;Mary&quot;);\n    stack.push(&quot;Frank&quot;);\n    stack.push(&quot;Joe&quot;);\n    stack.push(&quot;Sylvia&quot;);\n    stack.push(&quot;Vanessa&quot;);\n\n    while ( !stack.empty() )\n    {\n      String name = stack.pop(); \/\/ Note NO required cast\n      System.out.println(name);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p>This program outputs the following (same as the original):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Vanessa\nSylvia\nJoe\nFrank<\/strong><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Changing_the_Type\"><\/span>Changing the Type<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p>To show how you can change the type, here&#8217;s an example of using a stack with&nbsp;<strong>Integer<\/strong>&nbsp;elements:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage examples;\n\nimport java.util.Stack;\n\npublic class IntegerStackExample\n{\n  public static void main(String&#x5B;] args)\n  {\n    Stack&lt;Integer&gt; stack = new Stack&lt;Integer&gt;();\n    stack.push(1);\n    stack.push(2);\n    stack.push(3);\n    stack.push(4);\n    stack.push(5);\n\n    while ( !stack.empty() )\n    {\n      Integer value = stack.pop(); \/\/ Note NO required cast\n      System.out.println(value);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p>which produces the following output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>5\n4\n3\n2<\/strong><\/pre>\n\n\n\n<p>(Notice that I&#8217;m using &#8220;autoboxing&#8221; on both the input and the output.)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Comparable_Example\"><\/span>Comparable Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here&#8217;s the&nbsp;<strong>ComparableSorter<\/strong>&nbsp;example, translated to using generics:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage examples;\n\nimport java.util.Collections;\nimport java.util.ArrayList;\nimport java.util.Iterator;\nimport java.util.List;\n\npublic class ComparableSorter\n{\n  public static void main(String&#x5B;] args)\n  {\n    List&lt;String&gt; list = new ArrayList&lt;String&gt;();\n    list.add(&quot;abc&quot;);\n    list.add(&quot;DEF&quot;);\n    list.add(&quot;ghi&quot;);\n\n    \/\/ Do the sort\n    Collections.sort(list);\n\n    \/\/ See results\n    Iterator&lt;String&gt; iter = list.iterator();\n    while (iter.hasNext())\n    {\n      String item = iter.next(); \/\/ NO cast required\n      System.out.println(item);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p>which outputs (same as original):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>DEF\nabc\nghi<\/strong><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Comparator_Example\"><\/span>Comparator Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here&#8217;s the <strong>ComparatorSorter<\/strong> example, converted to using generics.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage examples;\n\nimport java.util.Collections;\nimport java.util.Comparator;\nimport java.util.ArrayList;\nimport java.util.Iterator;\nimport java.util.List;\n\npublic class ComparatorSorter\n{\n  public static void main(String&#x5B;] args)\n  {\n    List&lt;String&gt; list = new ArrayList&lt;String&gt;();\n    list.add(&quot;abc&quot;);\n    list.add(&quot;DEF&quot;);\n    list.add(&quot;ghi&quot;);\n\n    \/\/ Do the sort\n    Collections.sort(list, new CaseInsensitiveComparator() );\n\n    \/\/ See results\n    Iterator&lt;String&gt; iter = list.iterator();\n    while (iter.hasNext())\n    {\n      String item = iter.next();\n      System.out.println(item);\n    }\n  }\n}\n\nclass CaseInsensitiveComparator implements Comparator&lt;String&gt;\n{\n  public int compare(String s1, String s2)\n  {\n    return s1.toLowerCase().compareTo( s2.toLowerCase() );\n  }\n}\n<\/pre><\/div>\n\n\n<p>which outputs (same as the original):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>abc\nDEF\nghi<\/strong><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"HashSet_Example\"><\/span>HashSet Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here is the&nbsp;<strong>HashSet<\/strong>&nbsp;example, translated to use generics:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage examples;\n\nimport java.util.HashSet;\nimport java.util.Iterator;\n\npublic class HashSetExample\n{\n  public static void main(String&#x5B;] args)\n  {\n    HashSet&lt;String&gt; set = new HashSet&lt;String&gt;();\n    set.add(&quot;Mary&quot;);\n    set.add(&quot;Frank&quot;);\n    set.add(&quot;Joe&quot;);\n    set.add(&quot;Sylvia&quot;);\n    set.add(&quot;Vanessa&quot;);\n    set.add(&quot;Frank&quot;);   \/\/ Duplicate\n\n    for (Iterator&lt;String&gt; iter = set.iterator(); iter.hasNext(); )\n    {\n      String name = iter.next(); \/\/ Note NO required cast\n      System.out.println(name);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p>It outputs the following (same as the original):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Vanessa\nJoe\nFrank\nMary\nSylvia<\/strong><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"TreeSet_Example\"><\/span>TreeSet Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here&#8217;s the\u00a0<strong>TreeSet<\/strong>\u00a0example, translated to using generics:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage examples;\n\nimport java.util.Iterator;\nimport java.util.TreeSet;\n\npublic class TreeSetExample\n{\n  public static void main(String&#x5B;] args)\n  {\n    TreeSet&lt;String&gt; set = new TreeSet&lt;String&gt;();\n    set.add(&quot;Mary&quot;);\n    set.add(&quot;Frank&quot;);\n    set.add(&quot;Joe&quot;);\n    set.add(&quot;Sylvia&quot;);\n    set.add(&quot;Vanessa&quot;);\n    set.add(&quot;Frank&quot;);   \/\/ Duplicate\n\n    for (Iterator&lt;String&gt; iter = set.iterator(); iter.hasNext(); )\n    {\n      String name = iter.next(); \/\/ Note NO required cast\n      System.out.println(name);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p>Here&#8217;s the output of the example (same as the original):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Frank\nJoe\nMary\nSylvia\nVanessa<\/strong><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"TreeSet_with_Comparator\"><\/span>TreeSet with Comparator<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here is the\u00a0<strong>TreeSet<\/strong>\u00a0example with a\u00a0<strong>Comparator<\/strong>, converted to using generics:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage examples;\n\nimport java.util.Comparator;\nimport java.util.Iterator;\nimport java.util.TreeSet;\n\npublic class ReversedTreeSetExample\n{\n  public static void main(String&#x5B;] args)\n  {\n    TreeSet&lt;String&gt; set = new TreeSet&lt;String&gt;( new ReverseOrderComparator() );\n    set.add(&quot;Mary&quot;);\n    set.add(&quot;Frank&quot;);\n    set.add(&quot;Joe&quot;);\n    set.add(&quot;Sylvia&quot;);\n    set.add(&quot;Vanessa&quot;);\n    set.add(&quot;Frank&quot;);   \/\/ Duplicate\n\n    for (Iterator&lt;String&gt; iter = set.iterator(); iter.hasNext(); )\n    {\n      String name = iter.next(); \/\/ Note NO required cast\n      System.out.println(name);\n    }\n  }\n}\n\nclass ReverseOrderComparator implements Comparator&lt;String&gt;\n{\n  public int compare(String s1, String s2)\n  {\n    return -(s1.compareTo(s2));\n    \/\/ Reverse order by reversing comparison\n  }\n}\n<\/pre><\/div>\n\n\n<p>which outputs (same as original):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Vanessa\nSylvia\nMary\nJoe\nFrank<\/strong><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"HashMap_Example\"><\/span>HashMap Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here&#8217;s the\u00a0<strong>HashMap<\/strong>\u00a0class, converted to using generics:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage examples;\n\nimport java.util.HashMap;\nimport java.util.Iterator;\nimport java.util.Map;\n\npublic class HashMapExample\n{\n  public static void main(String&#x5B;] args)\n  {\n    HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();\n    map.put(&quot;Mary&quot;, &quot;555-1234&quot;);\n    map.put(&quot;Frank&quot;, &quot;555-5678&quot;);\n    map.put(&quot;Joe&quot;, &quot;555-9012&quot;);\n    map.put(&quot;Sylvia&quot;, &quot;555-3456&quot;);\n    map.put(&quot;Vanessa&quot;, &quot;555-7890&quot;);\n    map.put(&quot;Frank&quot;, &quot;555-0987&quot;);   \/\/ Duplicate\n\n    for (Iterator&lt;Map.Entry&lt;String, String&gt;&gt; iter = map.entrySet().iterator(); iter.hasNext(); )\n    {\n      Map.Entry&lt;String, String&gt; entry = iter.next(); \/\/ Note NO required cast\n      String key = entry.getKey(); \/\/ Note NO required cast\n      String value = entry.getValue(); \/\/ Note NO required cast\n      System.out.println(key + &quot;:&quot; + value);\n    }\n  }\n}\n\n<\/pre><\/div>\n\n\n<p>which outputs:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Vanessa:555-7890\nFrank:555-0987\nSylvia:555-3456\nMary:555-1234\nJoe:555-90<\/strong><\/pre>\n\n\n\n<p>(This is not the same as the original &#8212; it&#8217;s in a different order &#8212; but since it&#8217;s a <strong>HashMap<\/strong>, the order isn&#8217;t guaranteed, anyway.)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"TreeMap_Example\"><\/span>TreeMap Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here&#8217;s the\u00a0<strong>TreeMap<\/strong>\u00a0example, converted to using generics:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage examples;\n\nimport java.util.Iterator;\nimport java.util.Map;\nimport java.util.TreeMap;\n\npublic class TreeMapExample\n{\n  public static void main(String&#x5B;] args)\n  {\n    TreeMap&lt;String, String&gt; map = new TreeMap&lt;String, String&gt;();\n    map.put(&quot;Mary&quot;, &quot;555-1234&quot;);\n    map.put(&quot;Frank&quot;, &quot;555-5678&quot;);\n    map.put(&quot;Joe&quot;, &quot;555-9012&quot;);\n    map.put(&quot;Sylvia&quot;, &quot;555-3456&quot;);\n    map.put(&quot;Vanessa&quot;, &quot;555-7890&quot;);\n    map.put(&quot;Frank&quot;, &quot;555-0987&quot;);   \/\/ Duplicate\n\n    for (Iterator&lt;Map.Entry&lt;String, String&gt;&gt; iter = map.entrySet().iterator(); iter.hasNext(); )\n    {\n      Map.Entry&lt;String, String&gt; entry = iter.next(); \/\/ Note NO required cast\n      String key = entry.getKey(); \/\/ Note NO required cast\n      String value = entry.getValue(); \/\/ Note NO required cast\n      System.out.println(key + &quot;:&quot; + value);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p>It outputs (same as the original):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Frank:555-0987\nJoe:555-9012\nMary:555-1234\nSylvia:555-3456\nVanessa:555-7890<\/strong><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"TreeMap_with_Comparator\"><\/span>TreeMap with Comparator<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Here&#8217;s the <strong>ReversedTreeMap<\/strong> example, converted to using generics:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\npackage examples;\n\nimport java.util.Comparator;\nimport java.util.Iterator;\nimport java.util.Map;\nimport java.util.TreeMap;\n\npublic class ReversedTreeMapExample\n{\n  public static void main(String&#x5B;] args)\n  {\n    TreeMap&amp;lt;String, String&gt; map = \n          new TreeMap&amp;lt;String, String&gt;( new ReverseOrderKeyComparator() );\n    map.put(&quot;Mary&quot;, &quot;555-1234&quot;);\n    map.put(&quot;Frank&quot;, &quot;555-5678&quot;);\n    map.put(&quot;Joe&quot;, &quot;555-9012&quot;);\n    map.put(&quot;Sylvia&quot;, &quot;555-3456&quot;);\n    map.put(&quot;Vanessa&quot;, &quot;555-7890&quot;);\n    map.put(&quot;Frank&quot;, &quot;555-0987&quot;);   \/\/ Duplicate\n\n    for (Iterator&amp;lt;Map.Entry&amp;lt;String, String&gt;&gt; iter = map.entrySet().iterator(); iter.hasNext(); )\n    {\n      Map.Entry&amp;lt;String, String&gt; entry = iter.next(); \/\/ Note NO required cast\n      String key = entry.getKey(); \/\/ Note NO required cast\n      String value = entry.getValue(); \/\/ Note NO required cast\n      System.out.println(key + &quot;:&quot; + value);\n    }\n  }\n}\n\nclass ReverseOrderKeyComparator implements Comparator&amp;lt;String&gt;\n{\n  public int compare(String s1, String s2)\n  {\n    return -(s1.compareTo(s2));\n    \/\/ Reverse order by reversing comparison\n  }\n}\n<\/pre><\/div>\n\n\n<p>which outputs the following (same as original):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Vanessa:555-7890\nSylvia:555-3456\nMary:555-1234\nJoe:555-9012\nFrank:555-0987<\/strong><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Remember the Collections Framework? All of the Collections Framework classes and interfaces allow you to use generics (but they also allow you to choose not to use generics).&nbsp; As I think you&#8217;ll see, using generics is almost always the preferred approach. Vector\/Enumeration Example Here&#8217;s the&nbsp;Vector\/Enumeration&nbsp;example from before, translated to using generics: which outputs the following [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":77,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_eb_attr":"","_uag_custom_page_level_css":"","ocean_post_layout":"left-sidebar","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"ocs-course-topics-sidebar","ocean_second_sidebar":"0","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"0","ocean_custom_header_template":"0","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"0","ocean_menu_typo_font_family":"0","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"0","footnotes":""},"class_list":["post-1265","page","type-page","status-publish","hentry","entry"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"ocean-thumb-m":false,"ocean-thumb-ml":false,"ocean-thumb-l":false},"uagb_author_info":{"display_name":"Bryan Higgs","author_link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/author\/bryan\/"},"uagb_comment_info":0,"uagb_excerpt":"Remember the Collections Framework? All of the Collections Framework classes and interfaces allow you to use generics (but they also allow you to choose not to use generics).&nbsp; As I think you&#8217;ll see, using generics is almost always the preferred approach. Vector\/Enumeration Example Here&#8217;s the&nbsp;Vector\/Enumeration&nbsp;example from before, translated to using generics: which outputs the following&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/1265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/comments?post=1265"}],"version-history":[{"count":7,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/1265\/revisions"}],"predecessor-version":[{"id":1279,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/1265\/revisions\/1279"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/77"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=1265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}