<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CertPal &#187; navigableset</title>
	<atom:link href="http://www.certpal.com/blogs/tag/navigableset/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.certpal.com/blogs</link>
	<description>Technology and certifications</description>
	<lastBuildDate>Tue, 24 Aug 2010 17:47:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Java &#8211; Navigable Set</title>
		<link>http://www.certpal.com/blogs/2009/08/java-navigable-set/</link>
		<comments>http://www.certpal.com/blogs/2009/08/java-navigable-set/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 13:42:26 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[Java certifications]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[navigableset]]></category>
		<category><![CDATA[scjp]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=16</guid>
		<description><![CDATA[Sun introduced the NavigableMap class in JDK 6. This article details how to work with this class by providing a short tutorial.]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fjava-navigable-set%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fjava-navigable-set%2F" height="61" width="51" /></a></div><p><strong>Sets:</strong></p>
<p>A Set is a collection that contains no duplicate elements. Sets are collections where no two elements are the same, which means that element1.equals(element2) should never return true. Also at most one element in a set can be null and no more.</p>
<p><strong>A Set. Duplicates are not allowed and at most one null element is allowed</strong></p>
<p style="text-align: center"><strong><img class="aligncenter" src="http://www.certpal.com/images/set.JPG" alt="" width="138" height="269" /></strong></p>
<p><strong>Sorted Sets:</strong></p>
<p>A <strong>SortedSet </strong>is a set which sorts and maintains the ordering of its elements. Elements will be ordered by their natural ordering or by the use of a Comparator (if the developer provides one). A <a href="http://java.sun.com/javase/6/docs/api/java/lang/Comparable.html">Comparable</a> interface can help define the ordering for elements. For example a String’s natural ordering will dictate that elements be ordered this way “a”, “b”, “c” etc. The way in which elements are ordered in a Sorted data structure can be redefined by using the <a href="http://java.sun.com/javase/6/docs/api/java/util/Comparator.html">Comparator</a> interface.</p>
<p><strong> Sorted set where elements are sorted by natural order.</strong></p>
<p><strong><img class="aligncenter" src="http://www.certpal.com/images/sorted_set.JPG" alt="" width="86" height="258" /></strong></p>
<p><strong>Navigable set:</strong></p>
<p>A navigable set is a sorted set. Technically this means that a NavigableSet extends from a SortedSet. The navigable set has a couple of methods that will return the closest matching element given a search target. A NavigableSet can be accessed in the ascending or descending order. To get an ascending view of this structure the iterator() method can be used like in all Collection classes. To get a descending view of this structure use the descendingIterator() method. This method returns an Iterator whose elements are in the reverse natural order. That.s a fancy way of saying .in descending order. <img src='http://www.certpal.com/blogs/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The implementations of NavigableSet are the TreeSet and the ConcurrentSkipListSet. Let us take an example of the Iteration orders of the navigable set</p>
<p><strong>Code: </strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
    NavigableSet <span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span> navigableSet  <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> TreeSet<span style="color: #339933;">&lt;</span>Integer<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    navigableSet.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    navigableSet.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    navigableSet.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    navigableSet.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">40</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    navigableSet.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">90</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Iterator<span style="color: #339933;">&lt;</span> Integer<span style="color: #339933;">&gt;</span> iterator <span style="color: #339933;">=</span> navigableSet.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    Iterator<span style="color: #339933;">&lt;</span> Integer<span style="color: #339933;">&gt;</span> reverseIterator <span style="color: #339933;">=</span> navigableSet.<span style="color: #006633;">descendingIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Forward ahoy !&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>iterator.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>iterator.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Reverse yoha !&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>reverseIterator.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>reverseIterator.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Output:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">The output of <span style="color: #000000; font-weight: bold;">this</span> program would be<span style="color: #339933;">:</span>
Forward ahoy <span style="color: #339933;">!</span>
<span style="color: #339933;">-</span><span style="color: #cc66cc;">10</span>
<span style="color: #cc66cc;">10</span>
<span style="color: #cc66cc;">20</span>
<span style="color: #cc66cc;">40</span>
<span style="color: #cc66cc;">90</span>
&nbsp;
Reverse yoha <span style="color: #339933;">!</span>
<span style="color: #cc66cc;">90</span>
<span style="color: #cc66cc;">40</span>
<span style="color: #cc66cc;">20</span>
<span style="color: #cc66cc;">10</span>
<span style="color: #339933;">-</span><span style="color: #cc66cc;">10</span></pre></td></tr></table></div>

<p>As expected the Iterators iterate through the set in the natural and reverse natural order. Tree sets also automatically sort their elements. There are some key methods that make the NavigableSet useful in certain scenarios. We will have a look at them now.</p>
<p><strong>Key methods in the NavigableSet:</strong></p>
<p>Don&#8217;t worry if some of these methods do not make sense. We will take a look at them with an example</p>
<table style="height: 215px;" border="1" cellspacing="1" cellpadding="1" width="500" align="center">
<tbody>
<tr>
<td align="center"><strong> Method name</strong></td>
<td align="center"><strong> What element will It return ?</strong></td>
</tr>
<tr>
<td align="center">lower(element)</td>
<td align="center">Less than current element</td>
</tr>
<tr>
<td align="center">floor(element)</td>
<td align="center">Less than or equal to current element</td>
</tr>
<tr>
<td align="center">ceiling(element)</td>
<td align="center">Greater than or equal to current element</td>
</tr>
<tr>
<td align="center">higher(element)</td>
<td align="center">Greater than current element</td>
</tr>
<tr>
<td align="center">subSet(fromElem,isInclusive ,toElem, isInclusive)</td>
<td align="center">Returns a subset of the Set from an element to another. Flags can be set to mention if the elements should be inclusive or not</td>
</tr>
<tr>
<td align="center">headSet(toElem,isInclusive)</td>
<td align="center">A subset of this set whose elements are strictly less than toElem</td>
</tr>
<tr>
<td align="center">tailSet(fromElem,isInclusive)</td>
<td align="center">A subset of this set whose elements are greater than toElem</td>
</tr>
<tr>
<td align="center">descendingSet()</td>
<td align="center">Returns a set whose elements are in the descending order</td>
</tr>
<tr>
<td align="center">pollFirst()</td>
<td align="center">Retrieve and remove the first element</td>
</tr>
<tr>
<td align="center">pollLast()</td>
<td align="center">Retrieve and remove the last element</td>
</tr>
</tbody>
</table>
<p>A small program illustrates each method. Assume that a NavigableSet is inserted with the following data just like before.</p>
<p><strong> Values added into the set in this order:</strong></p>
<p><img class="alignnone" src="http://www.certpal.com/images/set_order.JPG" alt="" width="379" height="59" /></p>
<p>Let&#8217;s say the following lines were executed in the program</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>navigableSet.<span style="color: #006633;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>navigableSet.<span style="color: #006633;">ceiling</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>navigableSet.<span style="color: #006633;">lower</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>navigableSet.<span style="color: #006633;">higher</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>navigableSet.<span style="color: #006633;">higher</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">90</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>The output would be as follows:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #339933;">-</span><span style="color: #cc66cc;">10</span>
<span style="color: #cc66cc;">10</span>
<span style="color: #cc66cc;">20</span>
<span style="color: #cc66cc;">40</span>
<span style="color: #000066; font-weight: bold;">Null</span></pre></td></tr></table></div>

<p>To explain the output, let&#8217;s arrange the elements first, the way a sorted set would see them</p>
<p><img class="alignnone" src="http://www.certpal.com/images/set_arrange.JPG" alt="" width="391" height="57" /></p>
<p>When the Set encounters methods like floor(),ceiling() etc, this is what it translates to.</p>
<p><strong>floor(5) </strong>– Give me the element that is less than or equal to 5. Result is -10</p>
<p><strong>ceiling(5) </strong>- Give me the element that is greater than or equal to 5. Result is 10</p>
<p><strong>lower(30) </strong>- Give me the element that is less than 30. Result is 20</p>
<p><strong>higher(30) </strong>- Give me the element that is greater than 30. Result is 40</p>
<p>Any request for an element that does not match the criteria will result in a <strong>null </strong>return. For example a request to the set to return something higher than 90 gives null since 90 is the highest value in the set.</p>
<p><strong> More examples:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>navigableSet.<span style="color: #006633;">subSet</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span>,<span style="color: #000066; font-weight: bold;">true</span>,<span style="color: #cc66cc;">40</span>,<span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>navigableSet.<span style="color: #006633;">tailSet</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">10</span>, <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>navigableSet.<span style="color: #006633;">headSet</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">40</span>, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>navigableSet.<span style="color: #006633;">descendingSet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>The output:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">10</span>, <span style="color: #cc66cc;">20</span>, <span style="color: #cc66cc;">40</span><span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">10</span>, <span style="color: #cc66cc;">20</span>, <span style="color: #cc66cc;">40</span>, <span style="color: #cc66cc;">90</span><span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#91;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">10</span>, <span style="color: #cc66cc;">10</span>, <span style="color: #cc66cc;">20</span>, <span style="color: #cc66cc;">40</span><span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">90</span>, <span style="color: #cc66cc;">40</span>, <span style="color: #cc66cc;">20</span>, <span style="color: #cc66cc;">10</span>, <span style="color: #339933;">-</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#93;</span></pre></td></tr></table></div>

<p>Again, given the sorted set this result can be explained.</p>
<p><img class="alignnone" src="http://www.certpal.com/images/sorted_set_2.JPG" alt="" width="393" height="57" /></p>
<p><strong>subSet(10,true,40,true)</strong>- Give me all the elements between 10 and 40, inclusive of 10 and 40.</p>
<p><strong>tailSet(-10, false) </strong>- Give me all the elements after -10, exclusive of -10.</p>
<p><strong>headSet(40, true)</strong>- Give me all the elements before 40, inclusive of 40.</p>
<p><strong>descendingSet()</strong>- Show me the Navigable set in descending order</p>
<p>The descending set is nothing but the set in reverse order. It is helpful if you need the entire set in descending order instead of a descending Iterator to the set. That laves us with pollFirst() and pollLast().</p>
<p><strong>Polling through navigable sets:</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>navigableSet<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>navigableSet.<span style="color: #006633;">pollFirst</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>navigableSet.<span style="color: #006633;">pollLast</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>navigableSet<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p><strong>Output</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #009900;">&#91;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">10</span>, <span style="color: #cc66cc;">10</span>, <span style="color: #cc66cc;">20</span>, <span style="color: #cc66cc;">40</span>, <span style="color: #cc66cc;">90</span><span style="color: #009900;">&#93;</span>
<span style="color: #339933;">-</span><span style="color: #cc66cc;">10</span>
<span style="color: #cc66cc;">90</span>
<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">10</span>, <span style="color: #cc66cc;">20</span>, <span style="color: #cc66cc;">40</span><span style="color: #009900;">&#93;</span></pre></td></tr></table></div>

<p>The pollFirst() method retrieves and removes the first value in the set. Which is -10. The pollLast() method retrieves and removes the last value in the set, which is 90. As a result the set.s final size is reduced by 2. After removal of -10 and 90 from the set, the final set looks like this [10, 20, 40].</p>
<p>The <a href="http://java.sun.com/javase/6/docs/api/java/util/NavigableMap.html">NavigableMap</a> API is very similar to that of the NavigableSet (It is unfair to compare a Map and Set, but you get the picture). Now that you know how to use a NavigableSet, learning the NavigableMap should easy.</p>
<p><a href="../../index.html">Back to home</a></p>
<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fjava-navigable-set%2F"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fjava-navigable-set%2F" height="61" width="51" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2009/08/java-navigable-set/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>
