UNPKG

epubjs

Version:

Render ePub documents in the browser, across many devices

21 lines (20 loc) 3.48 kB
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Case Study: The sort() Method</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="sect1" title="Case Study: The sort() Method"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-8-SECT-12"/>Case Study: The sort() Method</h1></div></div></div><p><a id="idx10419" class="indexterm"/> <a id="idx10435" class="indexterm"/>Poking around in the <code class="literal">java.util.Collections</code> class, we find all kinds of static utility methods for working with collections. Among them is this goody—the static generic method <code class="literal">sort()</code>:</p><a id="I_8_tt480"/><pre class="programlisting"> <code class="o">&lt;</code><code class="n">T</code> <code class="kd">extends</code> <code class="n">Comparable</code><code class="o">&lt;?</code> <code class="kd">super</code> <code class="n">T</code><code class="o">&gt;&gt;</code> <code class="kt">void</code> <code class="n">sort</code><code class="o">(</code> <code class="n">List</code><code class="o">&lt;</code><code class="n">T</code><code class="o">&gt;</code> <code class="n">list</code> <code class="o">)</code> <code class="o">{</code> <code class="o">...</code> <code class="o">}</code></pre><p>Another nut for us to crack. Let’s focus on the last part of the bound:</p><a id="I_8_tt481"/><pre class="programlisting"> <code class="n">Comparable</code><code class="o">&lt;?</code> <code class="kd">super</code> <code class="n">T</code><code class="o">&gt;</code></pre><p>This is a wildcard instantiation of the <a id="I_indexterm8_id713814" class="indexterm"/><code class="literal">Comparable</code> interface, so we can read the <code class="literal">extends</code> as <code class="literal">implements</code> if it helps. <code class="literal">Comparable</code> holds a <a id="I_indexterm8_id713841" class="indexterm"/><code class="literal">compareTo()</code> method for some parameter type. A <code class="literal">Comparable&lt;String&gt;</code> means that the <code class="literal">compareTo()</code> method takes type <code class="literal">String</code>. Therefore, <code class="literal">Comparable&lt;? super T&gt;</code> is the set of instantiations of <code class="literal">Comparable</code> on <code class="literal">T</code> and all of its superclasses. A <code class="literal">Comparable&lt;T&gt;</code> suffices and, at the other end, so does a <code class="literal">Comparable&lt;Object&gt;</code>. What this means in English is that the elements must be comparable to their own type or some supertype of their own type. This is sufficient to ensure that the elements can all be compared to one another, but not as restrictive as saying that they must all implement the <code class="literal">compareTo()</code> method themselves. Some of the elements may inherit the <code class="literal">Comparable</code> interface from a parent class that knows how to compare only to a supertype of <code class="literal">T</code>, and that is exactly what is allowed here.<a id="I_indexterm8_id713919" class="indexterm"/><a id="I_indexterm8_id713926" class="indexterm"/></p></div></body></html>