{"id":539,"date":"2021-01-07T19:50:06","date_gmt":"2021-01-07T19:50:06","guid":{"rendered":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/?page_id=539"},"modified":"2021-01-07T21:49:58","modified_gmt":"2021-01-07T21:49:58","slug":"the-collections-framework","status":"publish","type":"page","link":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/","title":{"rendered":"The Collections Framework"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 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-6a3e4eff2d684\" 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-6a3e4eff2d684\"  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\/inheritance-polymorphism\/the-collections-framework\/#What_is_a_Collections_Framework\" >What is a Collections Framework?<\/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\/inheritance-polymorphism\/the-collections-framework\/#Vector_Enumeration\" >Vector &amp; Enumeration<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#Vector_Class\" >Vector Class<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#Enumeration_Interface\" >Enumeration Interface<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/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-6\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#Collections_Interfaces\" >Collections Interfaces<\/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\/inheritance-polymorphism\/the-collections-framework\/#The_Collection_Interface\" >The Collection Interface<\/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\/inheritance-polymorphism\/the-collections-framework\/#The_List_Interface\" >The List Interface<\/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\/inheritance-polymorphism\/the-collections-framework\/#The_Iterator_Interface\" >The Iterator Interface<\/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\/inheritance-polymorphism\/the-collections-framework\/#The_ListIterator_Interface\" >The ListIterator Interface<\/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\/inheritance-polymorphism\/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-12\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/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-13\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#The_Stack_Class\" >The Stack Class<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#Algorithms\" >Algorithms<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#The_Comparable_Interface\" >The Comparable Interface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#The_Comparator_Interface\" >The Comparator Interface<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#The_Collections_Class\" >The Collections Class<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#A_Comparable_Example\" >A Comparable Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#A_Comparator_Example\" >A Comparator Example<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#General_Applicability\" >General Applicability<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#Sets\" >Sets<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#The_Set_Interface\" >The Set Interface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#The_HashSet_Class\" >The HashSet Class<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#The_SortedSet_Interface\" >The SortedSet Interface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#The_TreeSet_Class\" >The TreeSet Class<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#TreeSet_with_Comparator\" >TreeSet with Comparator<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-27\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#Maps\" >Maps<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-28\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#The_Map_Interface\" >The Map Interface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-29\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#The_HashMap_Class\" >The HashMap Class<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-30\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#The_SortedMap_Interface\" >The SortedMap Interface<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-31\" href=\"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/course-topics\/inheritance-polymorphism\/the-collections-framework\/#TreeMap_with_Comparator\" >TreeMap with Comparator<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<p class=\"wp-block-paragraph\">So, we&#8217;ve learned about abstract classes and interfaces, right?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In reality, we&#8217;ll need some examples and some practice to see how they are typically used, before we can be comfortable using them in our own code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are many good examples of the use of abstract classes and interfaces in the Java class libraries.&nbsp; One particularly useful example is how they are used to implement the&nbsp;<em><strong>Java&nbsp;<\/strong><\/em><em><strong>Collections Framework.<\/strong><\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A collection is a &#8220;container&#8221; for a group of elements or items.&nbsp; It provides operations to allow manipulation of those elements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Java Collections Framework is represented by the following set of classes and interfaces (the tree structure represents the inheritance hierarchy):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Package java.util:<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Interfaces:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Collection\n    List\n    Set\n        SortedSet\nComparable (in package java.lang)\nComparator\nEnumeration\nIterator\n    ListIterator\nMap\n    SortedMap<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Classes:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Object\n    AbstractCollection (implements Collection)\n        AbstractList (implements List)\n            AbstractSequentialList\n                LinkedList\n            ArrayList\n            Vector\n                Stack\n        AbstractSet (implements Set)\n            HashSet\n            TreeSet (implements SortedSet)\n    AbstractMap (implements Map)\n        HashMap\n        TreeMap (implements SortedMap)\n        WeakHashMap\n    Dictionary\n        Hashtable (implements Map)\n            Properties<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">There&#8217;s clearly lots to talk about!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_a_Collections_Framework\"><\/span>What is a Collections Framework?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <em>Collections Framework<\/em> is a unified architecture for representing and manipulating collections.&nbsp; All collections frameworks include:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Interfaces:<\/strong>&nbsp;abstract data types representing collections.<\/li><li><strong>Implementations:<\/strong>&nbsp;concrete implementations of the collection interfaces.<\/li><li><strong>Algorithms:&nbsp;<\/strong>methods that perform useful computations (such as sorting and searching) on objects that implement collection interfaces.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The benefits of a <em>Collections Framework<\/em> are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Reduces programming effort<\/li><li>Increases program speed and quality<\/li><li>Allows interoperability among unrelated APIs<\/li><li>Reduces effort to learn and use new APIs<\/li><li>Reduces effort to design new APIs<\/li><li>Fosters software reuse<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Vector_Enumeration\"><\/span>Vector &amp; Enumeration<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>Vector<\/strong> class and the <strong>Enumeration<\/strong> interface have been part of Java since its very first version.&nbsp; They are heavily used in lots of Java code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, they represent an &#8220;old style&#8221; which has been replaced with a new set of classes and interfaces that were introduced in Java version 1.2.&nbsp; They still exist in the Java class library, but are deprecated in favor of the &#8220;new style&#8221;.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Vector_Class\"><\/span>Vector Class<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code><strong>Vector<\/strong><\/code>&nbsp;is a class that implements a kind of dynamic array.&nbsp;&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Object<\/strong>\n    <strong>Dictionary (abstract)\n        Hashtable\n            Properties\n    Vector\n        Stack<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can add instances of any class to it, using methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>add(&#8230;)<\/strong><\/li><li><strong>addElement(&#8230;)<\/strong><\/li><li>etc&#8230;<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">and then retrieve them using a number of methods:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>elementAt()<\/strong><\/li><li><strong>firstElement()<\/strong><\/li><li><strong>lastElement()<\/strong><\/li><li><strong>toArray()<\/strong><\/li><li>etc.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">You can remove elements from the&nbsp;<strong>Vector<\/strong>&nbsp;:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>remove()<\/strong><\/li><li><strong>removeElementAt()<\/strong><\/li><li><strong>removeAll()<\/strong><\/li><li><strong>removeRange()<\/strong><\/li><li>etc.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">and you can replace an element with another element:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>set()<\/strong><\/li><li><strong>setElementAt()<\/strong><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">You can also obtain the elements stored in a&nbsp;<code>Vector<\/code>&nbsp;by using an&nbsp;<code><strong>Enumeration<\/strong><\/code>, returned from the&nbsp;<code><strong>Vector<\/strong><\/code>&nbsp;by calling the method:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>elements()<\/strong><\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Note that&nbsp;<code>Vector<\/code>&nbsp;is being replaced by&nbsp;<code><strong>ArrayList<\/strong><\/code>.&nbsp; (see later).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Enumeration_Interface\"><\/span>Enumeration Interface<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code><strong>Enumeration<\/strong><\/code>&nbsp;is an interface which allows you to enumerate through a collection of items:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Enumeration<\/strong><\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage java.util;\n\n\/* Comments omitted *\/\npublic interface Enumeration \n{\n    \/**\n     * Tests if this enumeration contains more elements.\n     *\n     * @return  &lt;code&gt;true&lt;\/code&gt; if this enumeration contains more elements;\n     *          &lt;code&gt;false&lt;\/code&gt; otherwise.\n     * @since   JDK1.0\n     *\/\n    boolean hasMoreElements();\n\n    \/**\n     * Returns the next element of this enumeration.\n     *\n     * @return     the next element of this enumeration. \n     * @exception  NoSuchElementException  if no more elements exist.\n     * @since      JDK1.0\n     *\/\n    Object nextElement();\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><code><strong>Vector<\/strong><\/code>&#8216;s&nbsp;<code><strong>elements()<\/strong><\/code>&nbsp;method returns&nbsp;<em>an instance of a class that implements the&nbsp;<\/em><code><strong>Enumeration<\/strong><\/code><em>&nbsp;interface<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that&nbsp;<code><strong>Enumeration<\/strong><\/code>&nbsp;is being replaced by&nbsp;<code><strong>Iterator<\/strong><\/code>.&nbsp; (see later).<\/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 class=\"wp-block-paragraph\">Here&#8217;s an example of how one could use an instance of class&nbsp;<code><strong>Vector<\/strong><\/code>, and the interface&nbsp;<code><strong>Enumeration<\/strong><\/code>.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This code will run&nbsp;<em>under all versions of the Java SDK<\/em>.<\/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;\nimport java.util.Enumeration;\nimport java.util.Vector;\npublic class VectorExample\n{\n  public static void main(String&#x5B;] args)\n  {\n    Vector vector = new Vector();\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    for (int i = 0; i &lt; vector.size(); i++)\n    {\n      String name = (String) vector.elementAt(i); \/\/ Note required cast\n      System.out.println(i + &quot;: &quot; + name);\n    }\n    for (Enumeration en = vector.elements(); en.hasMoreElements(); )\n    {\n      String name = (String) en.nextElement(); \/\/ Note required cast\n      System.out.println(name);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which outputs the following:<\/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\n<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note the two ways of obtaining the elements of the vector:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Access each element by its index, using the&nbsp;<strong>elementAt(int index)<\/strong>&nbsp;method<\/li><li>Access each element through an&nbsp;<strong>Enumeration<\/strong>&nbsp;object, supplied by the&nbsp;<strong>Vector<\/strong>. (Remember,&nbsp;<strong>Enumeration&nbsp;<\/strong>is an interface.)<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This works, but is considered &#8220;old style&#8221;.&nbsp;&nbsp; So what&#8217;s the &#8220;new style&#8221;?&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To learn about that, you&#8217;ll have to learn some more background&#8230;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Collections_Interfaces\"><\/span>Collections Interfaces<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">There are a number of&nbsp;<em>collections framework interfaces<\/em>&nbsp;used to manipulate collections and pass them from one method to another.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Collection:<\/strong>&nbsp;Defines properties that all collections must have.&nbsp; For example, it defines a size() method to return the number of elements currently in the collection.<\/p>\n\n\n\n<p class=\"indent-text wp-block-paragraph\"><strong>List:&nbsp;<\/strong>Extends <strong>Collection<\/strong>, and adds additional properties that define ordered lists.&nbsp; For example, the ability to retrieve an element based on an index.&nbsp; (A list is sometimes called a&nbsp;<em>sequence<\/em>.)&nbsp; A List can contain duplicate elements.<\/p>\n\n\n\n<p class=\"indent-text wp-block-paragraph\"><strong>Set:&nbsp;<\/strong>Extends Collection, and adds additional properties that define a set.&nbsp; A set cannot contain duplicate elements.<\/p>\n\n\n\n<p class=\"double-indent-text wp-block-paragraph\"><strong>SortedSet:&nbsp;<\/strong>Extends Set, and adds support for ordering the set in a particular sequence.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Map:&nbsp;<\/strong>Defines properties that map keys to values.&nbsp; A Map cannot contain duplicate keys. (Note that a Map does not extend Collection, and so is not a true collection.)<\/p>\n\n\n\n<p class=\"indent-text wp-block-paragraph\"><strong>SortedMap:&nbsp;<\/strong>Extends Map, and adds properties that support ordering of the map in ascending key order.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Iterator:<\/strong>&nbsp;Defines the properties necessary to iterate through a Collection.<\/p>\n\n\n\n<p class=\"indent-text wp-block-paragraph\"><strong>ListIterator:<\/strong>&nbsp;Extends Iterator, and defines the properties necessary to iterate through a List.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Collection_Interface\"><\/span>The Collection Interface<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<strong>Collection<\/strong>&nbsp;interface (not to be confused with the&nbsp;<strong>Collections<\/strong>&nbsp;class &#8212; see later) defines the top-level methods for all collection objects (that is, all objects that implement the&nbsp;<strong>Collection<\/strong>&nbsp;interface).&nbsp;&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Collection\n    List\n    Set\n        SortedSet\nComparable (in java.lang package)\nComparator\nEnumeration\nIterator\n    ListIterator\nMap\n    SortedMap<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It defines the following methods (this is the original source, minus many comments):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage java.util;\n\npublic interface Collection \n{\n    \/\/ Query Operations\n\n    int size();\n    boolean isEmpty();\n    boolean contains(Object o);\n    Iterator iterator();\n    Object&#x5B;] toArray();\n    Object&#x5B;] toArray(Object a&#x5B;]);\n\n    \/\/ Modification Operations\n\n    boolean add(Object o);\n    boolean remove(Object o);\n\n    \/\/ Bulk Operations\n\n    boolean containsAll(Collection c);\n    boolean addAll(Collection c);\n    boolean removeAll(Collection c);\n    boolean retainAll(Collection c);\n    void clear();\n\n    \/\/ Comparison and hashing\n\n    boolean equals(Object o);\n    int hashCode();\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Note that the&nbsp;<code><strong>Collection<\/strong><\/code>&nbsp;interface itself does not enforce any policy, such as no duplicates or ordering.&nbsp; Such policies are enforced by sub-interfaces of the&nbsp;<code><strong>Collection<\/strong><\/code>&nbsp;interface.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_List_Interface\"><\/span>The List Interface<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>java.util.List<\/code>&nbsp;interface (not to be confused with the&nbsp;<strong>java.awt.List<\/strong>&nbsp;class, which represents an AWT GUI component):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Collection\n    <em>List<\/em>\n    Set\n        SortedSet\nComparable (in java.lang package)\nComparator\nEnumeration\nIterator\n    ListIterator\nMap\n    SortedMap<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code><strong>List<\/strong><\/code>&nbsp;interface defines&nbsp;<em>additional methods&nbsp;<\/em>which support the concept of&nbsp;<em>ordering&nbsp;<\/em>within the collection:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage java.util;\n\npublic interface List extends Collection \n{\n    \/\/ Modification Operations\n    boolean addAll(int index, Collection c);\n\n    \/\/ Positional Access Operations\n\n    Object get(int index);\n    Object set(int index, Object element);\n    void add(int index, Object element);\n    Object remove(int index);\n\n    \/\/ Search Operations\n\n    int indexOf(Object o);\n    int lastIndexOf(Object o);\n\n    \/\/ List Iterators\n\n    ListIterator listIterator();\n    ListIterator listIterator(int index);\n\n    \/\/ View\n\n    List subList(int fromIndex, int toIndex);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">A&nbsp;<code><strong>List<\/strong><\/code>&nbsp;defines methods which are analogous to what class&nbsp;<strong>Vector<\/strong>&nbsp;defined in JDK 1.1.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In JDK 1.2 and beyond,&nbsp;<strong>Vector<\/strong>&nbsp;extends the class&nbsp;<strong>AbstractList<\/strong>, which makes&nbsp;<strong>Vector<\/strong>&nbsp;a kind of&nbsp;<strong>List<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Object\n    AbstractCollection (implements Collection)\n        AbstractList (implements List)\n            AbstractSequentialList\n                LinkedList\n            ArrayList\n            Vector\n                Stack\n        AbstractSet (implements Set)\n            HashSet\n            TreeSet (implements SortedSet)\n    AbstractMap (implements Map)\n        HashMap\n        TreeMap (implements SortedMap)\n        WeakHashMap\n    Dictionary\n        Hashtable (implements Map)\n            Properties<\/strong><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Iterator_Interface\"><\/span>The Iterator Interface<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>Iterator<\/code>&nbsp;interface is designed to provide the interface for a class that can iterate through a collection.&nbsp;&nbsp;It is designed to replace the&nbsp;<code>Enumeration<\/code>&nbsp;interface<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Collection\n    List\n    Set\n        SortedSet\nComparable (in java.lang package)\nComparator\nEnumeration\n<em>Iterator<\/em>\n    ListIterator\nMap\n    SortedMap<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code><strong>Iterator<\/strong><\/code>&nbsp;interface defines the following methods:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage java.util;\n\npublic interface Iterator \n{\n    \/**\n     * Returns true if the iteration has more elements. (In other\n     * words, returns true if next would return an element\n     * rather than throwing an exception.)\n     *\n     * @return true if the iterator has more elements.\n     *\/\n    boolean hasNext();\n\n    \/**\n     * Returns the next element in the iteration.\n     *\n     * @returns the next element in the iteration.\n     * @exception NoSuchElementException iteration has no more elements.\n     *\/\n    Object next();\n\n    \/**\n     * \n     * Removes from the underlying collection the last element returned by the\n     * iterator (optional operation).  This method can be called only once per\n     * call to next.  The behavior of an iterator is unspecified if\n     * the underlying collection is modified while the iteration is in\n     * progress in any way other than by calling this method.\n     *\n     * @exception UnsupportedOperationException if the remove\n     *\t\t  operation is not supported by this Iterator.\n     \n     * @exception IllegalStateException if the next method has not\n     *\t\t  yet been called, or the remove method has already\n     *\t\t  been called after the last call to the next\n     *\t\t  method.\n     *\/\n    void remove();\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Note that it is reminiscent of&nbsp;<strong>Enumeration<\/strong>, but with two differences:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Method names have changed:<ul><li><strong>hasMoreElements()<\/strong>&nbsp;becomes&nbsp;<strong>hasNext()<\/strong><\/li><li><strong>nextElement()<\/strong>&nbsp;becomes&nbsp;<strong>next()<\/strong><\/li><\/ul><\/li><li><strong>Iterator<\/strong>&nbsp;allows the caller to remove elements from an underlying collection during the iteration, with well-defined semantics.&nbsp; (<strong>Enumeration<\/strong>&nbsp;does not support this.)<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_ListIterator_Interface\"><\/span>The ListIterator Interface<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<strong>ListIterator<\/strong>&nbsp;interface defines the additional methods necessary to support iteration though a <strong>List<\/strong>.&nbsp;&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Collection\n    List\n    Set\n        SortedSet\nComparable (in java.lang package)\nComparator\nEnumeration\nIterator\n    <em>ListIterator<\/em>\nMap\n    SortedMap<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">It adds the ability to traverse the list in either direction, and to modify the list during iteration:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage java.util;\n\npublic interface ListIterator extends Iterator \n{\n    \/\/ Query Operations\n\n    boolean hasPrevious();\n    Object previous();\n    int nextIndex();\n    int previousIndex();\n\n\n    \/\/ Modification Operations\n    \n    void set(Object o);\n    void add(Object o);\n}\n<\/pre><\/div>\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 class=\"wp-block-paragraph\">An ordered collection is called a&nbsp;<strong>List<\/strong>.&nbsp; A concrete&nbsp;<em>implementation<\/em>&nbsp;of a&nbsp;<strong>List<\/strong>&nbsp;is&nbsp;<strong>ArrayList<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Object\n    AbstractCollection (implements Collection)\n        AbstractList (implements List)\n            AbstractSequentialList\n                LinkedList\n            ArrayList\n            Vector\n                Stack\n        AbstractSet (implements Set)\n            HashSet\n            TreeSet (implements SortedSet)\n    AbstractMap (implements Map)\n        HashMap\n        TreeMap (implements SortedMap)\n        WeakHashMap\n    Dictionary\n        Hashtable (implements Map)\n            Properties<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ArrayList<\/strong>&nbsp;implements the&nbsp;<strong>List<\/strong>&nbsp;as a resizeable array, but that is just one possible implementation of a&nbsp;<strong>List<\/strong>.&nbsp; There are others.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how one could use an instance of class&nbsp;<strong>ArrayList<\/strong>.&nbsp; It will run&nbsp;<em>only under JDK 1.2 and beyond<\/em>:<\/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;\nimport java.util.ArrayList;\nimport java.util.Iterator;\npublic class ArrayListExample\n{\n  public static void main(String&#x5B;] args)\n  {\n    ArrayList list = new ArrayList();\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    for (int i = 0; i &lt; list.size(); i++)\n    {\n      String name = (String) list.get(i); \/\/ Note required cast\n      System.out.println(i + &quot;: &quot; + name);\n    }\n    for (Iterator iter = list.iterator(); iter.hasNext(); )\n    {\n      String name = (String) iter.next(); \/\/ Note required cast\n      System.out.println(name);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which produces the output:<\/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<p class=\"wp-block-paragraph\">The output is exactly the same as for the earlier&nbsp;<code><strong>Vector<\/strong><\/code>\/<code><strong>Enumeration<\/strong><\/code>&nbsp;example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Again, note the two ways of obtaining the elements of the list:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Access each element by its index, using the&nbsp;<strong>get(int index)<\/strong>&nbsp;method<\/li><li>Access each element through an&nbsp;<strong>Iterator<\/strong>&nbsp;object, supplied by the&nbsp;<strong>ArrayList<\/strong>. (Remember,&nbsp;<code>Iterator<\/code>&nbsp;is an interface.)<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">This is the &#8220;new style&#8221;.<\/p>\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 class=\"wp-block-paragraph\">Another concrete&nbsp;<em>implementation<\/em>&nbsp;of a&nbsp;<code>List<\/code>&nbsp;is&nbsp;<code>LinkedList<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Object\n    AbstractCollection (implements Collection)\n        AbstractList (implements List)\n            AbstractSequentialList\n                <em>LinkedList<\/em>\n            ArrayList\n            Vector\n                Stack\n        AbstractSet (implements Set)\n            HashSet\n            TreeSet (implements SortedSet)\n    AbstractMap (implements Map)\n        HashMap\n        TreeMap (implements SortedMap)\n        WeakHashMap\n    Dictionary\n        Hashtable (implements Map)\n            Properties<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>LinkedList<\/strong>&nbsp;implements the&nbsp;<strong>List<\/strong>, as you might expect, as a linked list of nodes.&nbsp; As a result, the performance of insertions into the middle of the list is expected to be better than that of the&nbsp;<code><strong>ArrayList<\/strong><\/code>.&nbsp; On the other hand, the performance of retrieving a particular entry in the list is expected to be better for&nbsp;<code><strong>ArrayList<\/strong><\/code>&nbsp;than for&nbsp;<code><strong>LinkedList<\/strong><\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When designing an application, it is very important to choose an appropriate implementation for the performance demands of your application.&nbsp; This choice can make a significant difference, sometimes by orders of magnitude!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of how one could use an instance of class&nbsp;<strong>LinkedList<\/strong>.&nbsp; It will run&nbsp;<em>only under JDK 1.2 and beyond<\/em>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [3,4,5,11,24]; 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 list = new LinkedList();\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 = (String) list.get(i); \/\/ Note required cast\n      System.out.println(i + &quot;: &quot; + name);\n    }\n\n    for (ListIterator iter = list.listIterator(); iter.hasNext(); )\n    {\n      String name = (String) iter.next(); \/\/ Note required cast\n      System.out.println(name);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Note the fact that I have changed the code in a somewhat subtle way:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>List list = new LinkedList();<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">as opposed to:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>LinkedList list = new LinkedList();<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Why did I do this?&nbsp; Why might it be a good thing?<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This program produces the output:<\/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<p class=\"wp-block-paragraph\">The output is exactly the same as for the earlier&nbsp;<code><strong>ArrayList<\/strong><\/code>&nbsp;example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Again, note the two ways of obtaining the elements of the list:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Access each element by its index, using the&nbsp;<code>get(int index)<\/code>&nbsp;method<\/li><li>Access each element through an&nbsp;<strong>ListIterator<\/strong>&nbsp;object, supplied by the&nbsp;<strong>LinkedList<\/strong>. (Remember,&nbsp;<strong>ListIterator<\/strong>&nbsp;is an interface.)<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Stack_Class\"><\/span>The Stack Class<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>Stack<\/code>&nbsp;class implements a&nbsp;<em><strong>Last In, First Out (LIFO) stack<\/strong><\/em>.&nbsp; It extends&nbsp;<code>Vector<\/code>, and so is a kind of&nbsp;<code>List<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Object\n    AbstractCollection (implements Collection)\n        AbstractList (implements List)\n            AbstractSequentialList\n                LinkedList\n            ArrayList\n            Vector\n                <em>Stack<\/em>\n        AbstractSet (implements Set)\n            HashSet\n            TreeSet (implements SortedSet)\n    AbstractMap (implements Map)\n        HashMap\n        TreeMap (implements SortedMap)\n        WeakHashMap\n    Dictionary\n        Hashtable (implements Map)\n            Properties<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Stack<\/strong>&nbsp;adds the following methods beyond what its super-classes provide:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage java.util;\n\npublic class Stack extends Vector \n{\n    \/\/ Constructs an empty stack\n    public Stack() {}\n    \/\/ Pushes an item onto the stack\n    public Object push(Object item) {...)\n    \/\/ Pops the top item off the stack\n    public synchronized Object pop() {...}\n    \/\/ Peeks at the top item on the stack, without removing it.\n    public synchronized Object peek() {...}\n    \/\/ Returns whether the stack is empty.\n    public boolean empty() {...}\n    \/\/ Returns the 1-based position where an object is on the stack.\n    public synchronized int search(Object o) {...}\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of the use of a&nbsp;<strong>Stack<\/strong>:<\/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 stack = new Stack();\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 = (String) stack.pop(); \/\/ Note required cast\n      System.out.println(name);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">This program outputs the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Vanessa\nSylvia\nJoe\nFrank\nMary<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">which is in the exact reverse order of the elements pushed onto the stack.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that I&#8217;ve used the&nbsp;<strong>empty<\/strong>&nbsp;method, which is in the JDK 1.1 version of&nbsp;<strong>Stack<\/strong>.&nbsp; If I had used the&nbsp;<strong>isEmpty<\/strong>&nbsp;method, this would have restricted the class to working only under JDK 1.2 and beyond.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Algorithms\"><\/span>Algorithms<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">As mentioned earlier, a collections framework provides <strong><em>interfaces<\/em>, <em>implementations<\/em> and <em>algorithms<\/em><\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An example of an algorithm is a&nbsp;<em><strong>sort<\/strong><\/em>.&nbsp;&nbsp; Sorting algorithms have been the subject of an immense amount of research in computer science over the years.&nbsp; You most likely have already studied several sort algorithms in a Data Structures and Algorithms class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By its very nature, a sort can only be applied to a collection that has a defined ordering.&nbsp; That is, one element of the collection can be compared against another element to determine which order to place them in.&nbsp; There are two interfaces which support such comparisons:&nbsp; <code><strong>Comparable<\/strong><\/code>&nbsp;and <code><strong>Comparator<\/strong><\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Comparable_Interface\"><\/span>The Comparable Interface<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<strong>Comparable<\/strong>&nbsp;interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class&#8217;s&nbsp;<em>natural ordering<\/em>, and the class&#8217;s&nbsp;<strong>compareTo<\/strong>&nbsp;method is referred to as its&nbsp;<em>natural comparison method<\/em>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Collection\n    List\n    Set\n        SortedSet\n<em>Comparable (in java.lang package)<\/em>\nComparator\nEnumeration\nIterator\n    ListIterator\nMap\n    SortedMap<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Comparable<\/strong>&nbsp;defines the following methods:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage java.lang;\n\npublic interface Comparable \n{\n    public int compareTo(Object o);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">where the&nbsp;<strong>compareTo<\/strong>&nbsp;method returns:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em><strong>a negative integer<\/strong><\/em>&nbsp;if this object is less then the specified object<\/li><li><em><strong>zero<\/strong><\/em>&nbsp;if this object is equal to the specified object<\/li><li><strong><em>a positive integer<\/em><\/strong>&nbsp;if this object greater than the specified object.<\/li><\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Comparator_Interface\"><\/span>The Comparator Interface<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<strong>Comparator<\/strong>&nbsp;interface is for imposing an ordering in the case where you might wish to compare two objects&nbsp;<em>that do not implement the<\/em>&nbsp;<strong>Comparable<\/strong>&nbsp;<em>interface<\/em>&nbsp;(or even on objects that do, if you wish).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Collection\n    List\n    Set\n        SortedSet\nComparable (in java.lang package)\n<em>Comparator<\/em>\nEnumeration\nIterator\n    ListIterator\nMap\n    SortedMap<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Comparator<\/strong>&nbsp;defines the following methods:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage java.util;\n\npublic interface Comparator \n{\n    int compare(Object o1, Object o2);\n    boolean equals(Object obj);\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">As with the&nbsp;<strong>Comparable compareTo<\/strong>&nbsp;method, the&nbsp;<strong>Comparator compare<\/strong>&nbsp;method returns:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em><strong>a negative integer<\/strong><\/em>&nbsp;if the first argument is less than the second argument<\/li><li><strong><em>zero<\/em><\/strong>&nbsp;if the first argument is equal to the second argument<\/li><li><strong><em>a positive integer<\/em><\/strong>&nbsp;if the first argument is greater than the second argument<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<strong>equals<\/strong>&nbsp;method may or may not need to be implemented in the class that implements&nbsp;<strong>Comparator<\/strong>, because the&nbsp;<strong>Object<\/strong>&nbsp;class implements such an&nbsp;<strong>equals<\/strong>&nbsp;method, which suffices for most purposes.&nbsp; However, you may wish to override this method to improve performance by allowing programs to determine that two distinct&nbsp;<strong>Comparator<\/strong>s impose the same order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Collections_Class\"><\/span>The Collections Class<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>Collections<\/code>&nbsp;class (in the&nbsp;<code>java.util<\/code>&nbsp;package) defines a set of methods and constants that are useful for working with collections and maps.&nbsp;&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Object<\/strong>\n    <strong><em>Collections<\/em><\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>Collections<\/code>&nbsp;class consists&nbsp;<em>only<\/em>&nbsp;of static methods and constants, and may not be instantiated. It should not be confused with the <code><strong>Collection<\/strong><\/code> (singular!) <em><strong>interface<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code><strong>Collections<\/strong><\/code>&nbsp;implements the following public methods:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage java.util;\n\npublic class Collections \n{\n    \/\/ Algorithms\n\n    \/\/ sort sorts the specified list into ascending order, \n    \/\/ according to the natural ordering of its elements.\n    \/\/ (uses a modified merge sort)\n    public static void sort(List list) {...}\n    public static void sort(List list, Comparator c) {...}\n    \/\/ binarySearch searches the specified list for the specified object \n    \/\/ using the binary search algorithm\n    public static int binarySearch(List list, Object key) {...}\n    public static int binarySearch(List list, Object key, Comparator c) {...}\n    \/\/ reverse reverses the order of the elements in the specified list.\n    public static void reverse(List l) {...}\n    \/\/ shuffle randomly permutes the specified list\n    public static void shuffle(List list) {...}\n    public static void shuffle(List list, Random rnd) {...}\n    \/\/ fill replaces all of the elements of the specified list \n    \/\/ with the specified element.\n    public static void fill(List list, Object o) {...}\n    \/\/ copy copies all of the elements from one list into another.\n    public static void copy (List dest, List src) {...}\n    \/\/ min returns the minimum element of the given collection, \n    \/\/ according to the natural ordering of its elements.\n    public static Object min(Collection coll) {...}\n    public static Object min(Collection coll, Comparator comp) {...}\n    \/\/ max returns the maximum element of the given collection, \n    \/\/ according to the natural ordering of its elements.\n    public static Object max(Collection coll) {...}\n    public static Object max(Collection coll, Comparator comp) {...}\n\n    \/\/ Unmodifiable Wrappers\n\n    public static Collection unmodifiableCollection(Collection c) {...}\n    public static Set unmodifiableSet(Set s) {...}\n    public static SortedSet unmodifiableSortedSet(SortedSet s) {...}\n    public static List unmodifiableList(List list) {...}\n    public static Map unmodifiableMap(Map m) {...}\n    public static SortedMap unmodifiableSortedMap(SortedMap m) {...}\n\n    \/\/ Synch Wrappers\n\n    public static Collection synchronizedCollection(Collection c) {...}\n    public static Set synchronizedSet(Set s) {...}\n    public static SortedSet synchronizedSortedSet(SortedSet s) {...}\n    public static List synchronizedList(List list) {...}\n    public static Map synchronizedMap(Map m) {...}\n    public static SortedMap synchronizedSortedMap(SortedMap m) {...}\n\n    \/\/ Miscellaneous\n\n    \/**\n     * The empty set (immutable).  This set is serializable.\n     *\/\n    public static final Set EMPTY_SET = ...;\n\n    \/**\n     * The empty list (immutable).  This list is serializable.\n     *\/\n    public static final List EMPTY_LIST = ...;\n\n    \/**\n     * Returns an immutable set containing only the specified object.\n     * The returned set is serializable.\n     *\n     * @return an immutable set containing only the specified object.\n     *\/\n    public static Set singleton(final Object o) {...}\n\n    \/**\n     * Returns an immutable list consisting of n copies of the\n     * specified object.  This method is useful in combination \n     * with the List.addAll method to grow lists.\n     * The returned list is serializable.\n     *\n    public static List nCopies(final int n, final Object o) {...}\n\n    \/**\n     * Returns a comparator that imposes the reverse of the natural\n     * ordering on a collection of objects that implement the\n     * Comparable interface.  (The natural ordering is the ordering\n     * imposed by the objects&#039; own compareTo method.)  This enables a\n     * simple idiom for sorting (or maintaining) collections (or arrays) of\n     * objects that implement the Comparable interface in\n     * reverse-natural-order.  For example, suppose a is an array of\n     * strings. Then:\n     * \t\tArrays.sort(a, Collections.reverseOrder());\n     * sorts the array in reverse-lexicographic (alphabetical) order.\n     *\n     * The returned comparator is serializable.\n     *\/\n    public static Comparator reverseOrder() {...}\n\n    \/**\n     * Returns an enumeration over the specified collection.  This provides\n     * interoperability with legacy APIs that require an enumeration\n     * as input.\n     *\/\n    public static Enumeration enumeration(final Collection c) {...}\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">where &#8220;<code>...<\/code>&#8221; represents implementation code, the details of which have been omitted.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"A_Comparable_Example\"><\/span>A Comparable Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of sorting, using the&nbsp;<code><strong>Comparable<\/strong><\/code>&nbsp;interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that&nbsp;<code><strong>String<\/strong><\/code>&nbsp;is defined to implement&nbsp;<code><strong>Comparable<\/strong><\/code>&nbsp;in JDK 1.2 and beyond.&nbsp; We are using&nbsp;<strong><code>String<\/code><\/strong>&#8216;s natural ordering, as defined by its&nbsp;<code><strong>compareTo<\/strong><\/code>&nbsp;method.<\/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 list = new ArrayList();\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 iter = list.iterator();\n    while (iter.hasNext())\n    {\n      System.out.println(iter.next());\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which outputs:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>DEF\nabc\nghi<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, the natural ordering for <strong>String<\/strong>s is case-sensitive.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"A_Comparator_Example\"><\/span>A Comparator Example<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of sorting using a&nbsp;<code><strong>Comparator<\/strong><\/code>&nbsp;interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It&#8217;s basically the same as the previous example, except that it uses a class that implements&nbsp;<code><strong>Comparator<\/strong><\/code><strong>.&nbsp;<\/strong>We&#8217;re using it to implement a&nbsp;<em>case-insensitive compare<\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In other words, if we want to sort using a non-natural ordering, we must use a&nbsp;<code><strong>Comparator<\/strong><\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [3,9,19,30,31,32,33,34,35,36,37,38]; 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 list = new ArrayList();\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 iter = list.iterator();\n    while (iter.hasNext())\n    {\n      System.out.println(iter.next());\n    }\n  }\n}\n\nclass CaseInsensitiveComparator implements Comparator\n{\n  public int compare(Object o1, Object o2)\n  {\n    String s1 = (String) o1;\n    String s2 = (String) o2;\n    return s1.toLowerCase().compareTo( s2.toLowerCase() );\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which outputs:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>abc\nDEF\nghi<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can see the changes from the previous example,&nbsp;highlighted.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"General_Applicability\"><\/span>General Applicability<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">I could add a number of examples, using other algorithms such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>binarySearch<\/li><li>reverse<\/li><li>shuffle<\/li><li>fill<\/li><li>copy<\/li><li>min<\/li><li>max<\/li><li>etc.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">but the main point of this collections framework is&nbsp;<strong><em>consistency<\/em><\/strong>:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>If you know how to call one algorithm, you know how to use them all;&nbsp; only minor changes have to be made.<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">This makes learning the framework a breeze, once you&#8217;ve learned the basics!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Sets\"><\/span>Sets<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Mathematically, a <em>set<\/em> is a collection of elements each of which has a unique value.&nbsp; That is, the value of no element in the set duplicates the value of another element in the set.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example,&nbsp;<strong>{1,2,3,4}<\/strong>&nbsp;is a set, because every element is unique.&nbsp; On the other hand,&nbsp;<strong>{a,b,c,d,e,a}<\/strong>&nbsp;is not a set, because there are two elements &#8216;a&#8217;.The <em>Collections Framework<\/em> includes support for such sets.&nbsp; Here&#8217;s how&#8230;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Set_Interface\"><\/span>The Set Interface<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>Set<\/code>&nbsp;interface enforces the rule that the collection not contain duplicates:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Collection\n    List\n   <em> Set<\/em>\n        SortedSet\nComparable (in java.lang package)\nComparator\nEnumeration\nIterator\n    ListIterator\nMap\n    SortedMap<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note that&nbsp;<code><strong>Set<\/strong><\/code>&nbsp;is a sub-interface of&nbsp;<code><strong>Collection<\/strong><\/code>.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_HashSet_Class\"><\/span>The HashSet Class<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code><strong>HashSet<\/strong><\/code>&nbsp;class is an implementation of&nbsp;<code><strong>Set<\/strong><\/code>&nbsp;that uses a hash table:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Object\n    AbstractCollection (implements Collection)\n        AbstractSet (implements Set)\n            <em>HashSet<\/em><\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here is our previous example that used&nbsp;<strong>ArrayList<\/strong>, changed to using&nbsp;<strong>HashSet<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The example only uses the <strong>Iterator<\/strong> approach to obtaining the elements from the collection, because there is no inherent ordering in a Set.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that I have added an additional call to <strong>add<\/strong>, which attempts to add a duplicate element for &#8220;Frank&#8221;.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [16]; 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 set = new HashSet();\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 iter = set.iterator(); iter.hasNext(); )\n    {\n      String name = (String) iter.next(); \/\/ Note required cast\n      System.out.println(name);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">It outputs the following:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Vanessa\nJoe\nFrank\nMary\nSylvia<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">which shows that no duplicate element was added.&nbsp; It also shows that the contents are returned in no particular order<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_SortedSet_Interface\"><\/span>The SortedSet Interface<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If you want a set that&nbsp;<strong><em>is<\/em><\/strong>&nbsp;ordered, then you can use a&nbsp;<code><strong>SortedSet<\/strong><\/code>&nbsp;interface:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Collection\n    Set\n        <em>SortedSet<\/em><\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code><strong>SortedSet<\/strong><\/code>&nbsp;interface adds the following methods to those already specified by&nbsp;<code><strong>Set<\/strong><\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Method<\/th><th>Description<\/th><\/tr><tr><td><code><strong>comparator()<\/strong><\/code><\/td><td>Returns the comparator associated with this sorted set, or null if it uses its elements&#8217; natural ordering.<\/td><\/tr><tr><td><code><strong>subSet(fromElement, toElement)<\/strong><\/code><\/td><td>Returns a view of the portion of this sorted set whose elements range&nbsp; from fromElement, inclusive, to toElement, exclusive. (If fromElement and toElement are equal, the returned sorted set is empty.)&nbsp;<\/td><\/tr><tr><td><code><strong>headSet(toElement)<\/strong><\/code><\/td><td>Returns a view of the portion of this sorted set whose elements are strictly less than toElement.<\/td><\/tr><tr><td><code><strong>tailSet(fromElement)<\/strong><\/code><\/td><td>Returns a view of the portion of this sorted set whose elements are greater than or equal to fromElement.<\/td><\/tr><tr><td><code><strong>first()<\/strong><\/code><\/td><td>Returns the first (lowest) element currently in this sorted set.<\/td><\/tr><tr><td><code><strong>last()<\/strong><\/code><\/td><td>Returns the last (highest) element currently in this sorted set.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_TreeSet_Class\"><\/span>The TreeSet Class<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">An implementation of a&nbsp;<code><strong>SortedSet<\/strong><\/code>&nbsp;is the&nbsp;<code><strong>TreeSet<\/strong><\/code>&nbsp;class, which uses a tree as its implementation:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Object\n    AbstractCollection (implements Collection)\n        AbstractSet (implements Set)\n            <em>TreeSet (implements SortedSet)<\/em><\/strong><\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [4,6,10]; 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 set = new TreeSet();\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 iter = set.iterator(); iter.hasNext(); )\n    {\n      String name = (String) iter.next(); \/\/ Note required cast\n      System.out.println(name);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Note the minimal changes to the program from the earlier <strong>HashSet<\/strong> example. This is a consequence of the consistent use of interfaces, and having different implementations that use the same interface.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s the output of the example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Frank\nJoe\nMary\nSylvia\nVanessa<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">which shows that we still have no duplicates, but that the elements are now returned in the proper order (in this case, alphabetically).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"TreeSet_with_Comparator\"><\/span>TreeSet with Comparator<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If we wish, we can impose a different ordering, by specifying a <code><strong>Comparator<\/strong><\/code>.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the following example, we reverse the ordering by the use of an explicit&nbsp;<code><strong>ReverseOrderComparator<\/strong><\/code>&nbsp;class:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [3,7,11,27,28,29,30,31,32,33,34,35,36]; 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 set = new TreeSet( 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 iter = set.iterator(); iter.hasNext(); )\n    {\n      String name = (String) iter.next(); \/\/ Note required cast\n      System.out.println(name);\n    }\n  }\n}\n\nclass ReverseOrderComparator implements Comparator\n{\n  public int compare(Object o1, Object o2)\n  {\n    String s1 = (String) o1;\n    String s2 = (String) o2;\n    return -(s1.compareTo(s2));\n    \/\/ Reverse order by reversing comparison\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which outputs:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Vanessa\nSylvia\nMary\nJoe\nFrank<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">As you can see, the results are in reverse alphabetical order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Maps\"><\/span>Maps<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <em>map<\/em> is an object that <em>maps<\/em> <em>keys<\/em> to <em>values<\/em>. A map cannot contain duplicate keys; each key can map to at most one value.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s where we see how the <em>Collections Framework<\/em> supports maps.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_Map_Interface\"><\/span>The Map Interface<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<strong>Map<\/strong>&nbsp;interface defines methods that map keys to values.&nbsp; A&nbsp;<strong>Map<\/strong>&nbsp;cannot contain duplicate keys.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that&nbsp;<strong>Map<\/strong>&nbsp;is at the root of a distinct interface hierarchy, and so it is not strictly speaking a true collection.&nbsp;&nbsp;&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Collection\n    List\n    Set\n        SortedSet\nComparable (in java.lang package)\nComparator\nEnumeration\nIterator\n    ListIterator\n<em>Map<\/em>\n    SortedMap<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A&nbsp;<strong>Map<\/strong>&nbsp;is not really a collection of elements, but a mapping of keys to values.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Map<\/strong>&nbsp;defines the following methods:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; title: ; quick-code: false; notranslate\" title=\"\">\npackage java.util;\n\npublic interface Map \n{\n    \/\/ Query Operations\n\n    int size();\n    boolean isEmpty();\n    boolean containsKey(Object key);\n    boolean containsValue(Object value);\n    Object get(Object key);\n\n    \/\/ Modification Operations\n\n    Object put(Object key, Object value);\n    Object remove(Object key);\n\n    \/\/ Bulk Operations\n\n    void putAll(Map t);\n    void clear();\n\n    \/\/ Views\n\n    \/**\n     * Returns a set view of the keys contained in this map.  \n     *\/\n    public Set keySet();\n\n    \/**\n     * Returns a collection view of the values contained in this map.  \n     *\/\n    public Collection values();\n\n    \/**\n     * Returns a set view of the mappings contained in this map.  \n     *\/\n    public Set entrySet();\n\n    \/**\n     * A map entry (key-value pair).  \n     *\/\n    public interface Entry \n    {\n\tObject getKey();\n\tObject getValue();\n\tObject setValue(Object value);\n\tboolean equals(Object o);\n\tint hashCode();\n    }\n\n    \/\/ Comparison and hashing\n\n    \/**\n     * Compares the specified object with this map for equality.  \n     *\/\n    boolean equals(Object o);\n\n    \/**\n     * Returns the hash code value for this map.  \n     *\/\n    int hashCode();\n}\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_HashMap_Class\"><\/span>The HashMap Class<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code>HashMap<\/code>&nbsp;class implements the&nbsp;<code><strong>Map<\/strong><\/code>&nbsp;interface, using a hash table:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Object\n    AbstractMap (implements Map)\n        <em>HashMap<\/em><\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Because of the implementation, the entries in a&nbsp;<code><strong>HashMap<\/strong><\/code>&nbsp;have no inherent ordering.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of using the&nbsp;<code><strong>HashMap<\/strong><\/code>&nbsp;class.&nbsp;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note that in this example the key is the person&#8217;s name, and the value is the person&#8217;s telephone number.&nbsp; In general, the key and the associated value are not limited to strings:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [3,5,11,12,13,14,15,16,17,19,21,22,23,24]; 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 map = new HashMap();\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 iter = map.entrySet().iterator(); iter.hasNext(); )\n    {\n      Map.Entry entry = (Map.Entry) iter.next(); \/\/ Note required cast\n      String key = (String) entry.getKey(); \/\/ Note required cast\n      String value = (String) entry.getValue(); \/\/ Note required cast\n      System.out.println(key + &quot;:&quot; + value);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which outputs:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Vanessa:555-7890\nJoe:555-9012\nFrank:555-0987\nMary:555-1234\nSylvia:555-3456<\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note the similarities to the previous code.&nbsp; This is not accidental.&nbsp; Again, it results from the consistent use of the appropriate set of interfaces.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, we can tell from the output of the program that the final&nbsp;<code><strong>put<\/strong><\/code>&nbsp;has the effect of&nbsp;<strong><em>replacing<\/em><\/strong>&nbsp;the value for Frank that was entered earlier.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In particular, note the fact that a&nbsp;<code><strong>Map<\/strong><\/code>&nbsp;does not have a direct way of obtaining an iterator.&nbsp; Instead, it defines an interface,&nbsp;<code><strong>Entry<\/strong><\/code>, nested within the&nbsp;<code><strong>Map<\/strong><\/code>&nbsp;class.&nbsp; It also defines a method&nbsp;<code><strong>entrySet()<\/strong><\/code>&nbsp;which returns a&nbsp;<code><strong>Set<\/strong><\/code>&nbsp;object (that is, an instance of a class that implements&nbsp;<code><strong>Set<\/strong><\/code>) corresponding to the entries in the&nbsp;<code><strong>Map<\/strong><\/code>&nbsp; You can then obtain an iterator from that&nbsp;<code><strong>Set<\/strong><\/code>&nbsp;object.&nbsp; The iterator returns&nbsp;<code><strong>Entry<\/strong><\/code>&nbsp;objects, from which you can obtain the key and value.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"The_SortedMap_Interface\"><\/span>The SortedMap Interface<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code><strong>SortedMap<\/strong><\/code>&nbsp;interface is a sub-interface of&nbsp;<code><strong>Map<\/strong><\/code>.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code><strong>SortedMap<\/strong><\/code>&nbsp;is a map that further guarantees that it will be held in ascending key order, sorted according to the natural ordering of its keys (see the&nbsp;<code><strong>Comparable<\/strong><\/code>&nbsp;interface), or by a comparator provided at sorted map creation time.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code><strong>SortedMap<\/strong><\/code>&nbsp;adds the following methods to those specified by&nbsp;<code><strong>Map<\/strong><\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><th>Method<\/th><th>Description<\/th><\/tr><tr><td><code><strong>comparator()<\/strong><\/code><\/td><td>Returns the comparator associated with this sorted map, or null if it uses its keys&#8217; natural ordering.<\/td><\/tr><tr><td><code><strong>subMap(fromKey, toKey)<\/strong><\/code><\/td><td>Returns a view of the portion of this sorted map whose keys range from fromKey, inclusive, to toKey, exclusive.<\/td><\/tr><tr><td><code><strong>headMap(toKey)<\/strong><\/code><\/td><td>Returns a view of the portion of this sorted map whose keys are strictly less than toKey.<\/td><\/tr><tr><td><code><strong>tailMap(fromKey)<\/strong><\/code><\/td><td>Returns a view of the portion of this sorted map whose keys are greater than or equal to fromKey.<\/td><\/tr><tr><td><code><strong>firstKey()<\/strong><\/code><\/td><td>Returns the first (lowest) key currently in this sorted map.<\/td><\/tr><tr><td><code><strong>lastKey()<\/strong><\/code><\/td><td>Returns the last (highest) key currently in this sorted map.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">The TreeMap Class<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<code><strong>TreeMap<\/strong><\/code>&nbsp;class implements a&nbsp;<code><strong>SortedMap<\/strong><\/code>, which means that it keeps the entries in key order.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>TreeMap<\/code>&nbsp;uses a tree to implement the&nbsp;<code>SortedMap<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Object\n    AbstractMap (implements Map)\n        <em>TreeMap (implements SortedMap)<\/em><\/strong><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here&#8217;s an example of its use:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [5,7,11]; 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 map = new TreeMap();\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 iter = map.entrySet().iterator(); iter.hasNext(); )\n    {\n      Map.Entry entry = (Map.Entry) iter.next(); \/\/ Note required cast\n      String key = (String) entry.getKey(); \/\/ Note required cast\n      String value = (String) entry.getValue(); \/\/ Note required cast\n      System.out.println(key + &quot;:&quot; + value);\n    }\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which, as you can see, is minimally different from the&nbsp;<strong>HashMap<\/strong>&nbsp;example.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It outputs:<\/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<p class=\"wp-block-paragraph\">which is in order by key, and also shows that the value associated with the second key value of &#8220;Frank&#8221; is the one that survives, causing the original &#8220;Frank&#8221; to be removed.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"TreeMap_with_Comparator\"><\/span>TreeMap with Comparator<span class=\"ez-toc-section-end\"><\/span><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">To change the order in which the&nbsp;<code><strong>TreeMap<\/strong><\/code>&nbsp;orders its elements, you can use a&nbsp;<code><strong>Comparator<\/strong><\/code>&nbsp;object, as before:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; auto-links: false; highlight: [3,12,30,31,32,33,34,35,36,37,38,39]; title: ; quick-code: false; 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 map = new TreeMap( 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 iter = map.entrySet().iterator(); iter.hasNext(); )\n    {\n      Map.Entry entry = (Map.Entry) iter.next(); \/\/ Note required cast\n      String key = (String) entry.getKey(); \/\/ Note required cast\n      String value = (String) entry.getValue(); \/\/ Note required cast\n      System.out.println(key + &quot;:&quot; + value);\n    }\n  }\n}\n\nclass ReverseOrderKeyComparator implements Comparator\n{\n  public int compare(Object o1, Object o2)\n  {\n    String s1 = (String) o1;\n    String s2 = (String) o2;\n    return -(s1.compareTo(s2));\n    \/\/ Reverse order by reversing comparison\n  }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">which outputs the following:<\/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\n\n\n<p class=\"wp-block-paragraph\">showing the expected reverse order by key.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, we&#8217;ve learned about abstract classes and interfaces, right? In reality, we&#8217;ll need some examples and some practice to see how they are typically used, before we can be comfortable using them in our own code. There are many good examples of the use of abstract classes and interfaces in the Java class libraries.&nbsp; One [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":61,"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-539","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":"So, we&#8217;ve learned about abstract classes and interfaces, right? In reality, we&#8217;ll need some examples and some practice to see how they are typically used, before we can be comfortable using them in our own code. There are many good examples of the use of abstract classes and interfaces in the Java class libraries.&nbsp; One&hellip;","_links":{"self":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/539","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=539"}],"version-history":[{"count":16,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/539\/revisions"}],"predecessor-version":[{"id":570,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/539\/revisions\/570"}],"up":[{"embeddable":true,"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/pages\/61"}],"wp:attachment":[{"href":"https:\/\/bhiggs.x10hosting.com\/PracticalJavaProgramming\/wp-json\/wp\/v2\/media?parent=539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}