<?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; scjp</title>
	<atom:link href="http://www.certpal.com/blogs/tag/scjp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.certpal.com/blogs</link>
	<description>Technology and certifications</description>
	<lastBuildDate>Mon, 18 Jul 2011 06:48:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Java &#8211; Console</title>
		<link>http://www.certpal.com/blogs/2009/08/java-console/</link>
		<comments>http://www.certpal.com/blogs/2009/08/java-console/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 11:14:07 +0000</pubDate>
		<dc:creator>CertPal</dc:creator>
				<category><![CDATA[Java certifications]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[scjp]]></category>

		<guid isPermaLink="false">http://www.certpal.com/blogs/?p=8</guid>
		<description><![CDATA[Sun introduced the java.io.console class to enable developers to obtain passwords / other sensitive data from the console / terminal. This article delves into the class a little.]]></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-console%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fjava-console%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>To get sensitive user input without echoing it to the 						console/terminal, the Java SDK has introduced the <strong> java.io.Console class.</strong> The Console class is quite unique 						in the way it handles its data. It uses the native encoding of the 						system instead of the using the JVM&#8217;s default encoding.</p>
<p>The Console class provides methods to access the console/terminal, if any is 							associated with the JVM.</p>
<p><strong>Using the Console class:</strong><br />
So how do you use the Console class ? Its pretty simple really. 						First get a reference to the Console class from the System class</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Console con <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">console</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It is not always possible to get a reference to the console. Some 						scenarios prevent the developer from gaining access to the 						console. This method may return null in these cases. For example a batch 							process running on a server will not expect user input and 							usually does have a console. Your code must be capable of handling such situations.</p>
<p>Consider that you have a program that requires the user to secretly type in his/her password.</p>
<p><strong> Entering sensitive data through the console:</strong><br />
<strong> </strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Console con <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">console</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> user <span style="color: #339933;">=</span> con.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Username: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> password <span style="color: #339933;">=</span> con.<span style="color: #006633;">readPassword</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Password: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>If you were to run this code using the eclipse IDE it would not 						work (At least in eclipse 3.2).<br />
This is because eclipse does not handle this code properly as 							yet. Run this code from the command prompt for 							now. Here is the output of the program</p>
<p><strong> Output:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">java com.<span style="color: #006633;">Tests</span>.<span style="color: #006633;">Cert</span>
Username<span style="color: #339933;">:</span> hello
Password<span style="color: #339933;">:</span></pre></div></div>

<p>Even though the output does not show a password being typed in, 						the user has typed it in on the console. The console&#8217;s 						feature allows the user to type in the password without it being 						echoed to the screen.</p>
<p>The line of code con.readLine(&#8220;Username: &#8220;); reads data from the 							console normally and stores it into a string. It also 							echoes whatever the user types onto the screen (you can see 							&#8220;hello&#8221; on the output). The line of code 							con.readPassword(&#8220;Password: &#8220;); prompts the user for a password 							but does not echo the contents of the password to the screen. 							This allows the user to enter his/her password safely using the 							Console class. To format the output to the screen the following 							code can be used.</p>
<p><strong> Formatting data:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Console con <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">console</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> user <span style="color: #339933;">=</span> con.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Welcome %s. Please login <span style="color: #000099; font-weight: bold;">\n</span>Username:&quot;</span>,<span style="color: #0000ff;">&quot;Guest&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> password <span style="color: #339933;">=</span> con.<span style="color: #006633;">readPassword</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;What is your password %s:&quot;</span>,user<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;User: &quot;</span> <span style="color: #339933;">+</span> user <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;Password: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span> <span style="color: #009900;">&#40;</span>password<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">java com.<span style="color: #006633;">Tests</span>.<span style="color: #006633;">Cert</span>
Welcome Guest. <span style="color: #006633;">Please</span> login
Username<span style="color: #339933;">:</span> hello
What is your password hello<span style="color: #339933;">:</span>
User<span style="color: #339933;">:</span> hello
Password<span style="color: #339933;">:</span> bla</pre></div></div>

<p>Notice that although the password is not visible in the console 						screen, the program was able to retrieve it. Also the line of code 						con.readPassword(&#8220;What is your password %s: &#8220;,user); will format 						the output such that the username is present when asking for the 						password. %s represents the String that was passed to the method.                          Now let&#8217;s take a look at using the format() method.</p>
<p><strong>Formatting output only: </strong></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Console con <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">console</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
con.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Welcome %s <span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, <span style="color: #0000ff;">&quot;Mr bond&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">String</span> user <span style="color: #339933;">=</span> con.<span style="color: #006633;">readLine</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Username: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> password <span style="color: #339933;">=</span> con.<span style="color: #006633;">readPassword</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;What is your password %s:&quot;</span>,user<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;User: &quot;</span> <span style="color: #339933;">+</span> user <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;Password: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span> <span style="color: #009900;">&#40;</span>password<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Welcome Mr bond
Username<span style="color: #339933;">:</span> 007
What is your password 007<span style="color: #339933;">:</span>
User<span style="color: #339933;">:</span> 007
Password<span style="color: #339933;">:</span> goldeneye</pre></div></div>

<p>In this case the formatting that was present in the readLine() 						method has been transferred to the format() method. Console also 						has a printf() method that works similar to the format() method. 						This method however has been made available only for convenience. 						Finally you can make use of the reader and writer classes to read 						and write to the Console.</p>
<p><strong> Reader and Writer: </strong><br />
Note: This code throws IOException since we need read/write access to the console</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">char</span> buffer<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">char</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
Console con <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">console</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Reader</span> reader <span style="color: #339933;">=</span> con.<span style="color: #006633;">reader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">int</span> numRead <span style="color: #339933;">=</span> reader.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// #1</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: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span> <span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, numRead<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">Writer</span> writer <span style="color: #339933;">=</span> con.<span style="color: #006633;">writer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// #2</span>
writer.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>buffer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
writer.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
writer.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

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

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">reader <span style="color: #339933;">--</span> #a. <span style="color: #006633;">Input</span> provided by a user
reader <span style="color: #339933;">--</span> #b. <span style="color: #006633;">Written</span> by <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>
reader <span style="color: #339933;">--</span> #c. <span style="color: #006633;">Written</span> by console writer</pre></div></div>

<p>The #a reader is the input that was provided to the program. The 						program reads this data using the Reader class. It reads the data 						into a character buffer in line #1 and prints the data to the console 						. At line #2 the console&#8217;s writer is obtained by the program 						and the contents of the character buffer are sent to the console 						output again (this time using System.out). Be wary however, when you 						use the Reader and Writer classes. They throw the IOException and 						since this is a checked Exception it must be handled. The 						code above throws this exception from a method (not shown) and thus need not 						handle it.</p>
<p><a href="../../index.html">Back to home</a>
<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-console%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fjava-console%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2009/08/java-console/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<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"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fjava-navigable-set%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</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"><div 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></div></div>

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

<div class="wp_syntax"><div 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></div></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"><div 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></div></div>

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

<div class="wp_syntax"><div 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></div></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"><div 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></div></div>

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

<div class="wp_syntax"><div 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></div></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"><div 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></div></div>

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

<div class="wp_syntax"><div 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></div></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>
<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"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.certpal.com%2Fblogs%2F2009%2F08%2Fjava-navigable-set%2F&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
]]></content:encoded>
			<wfw:commentRss>http://www.certpal.com/blogs/2009/08/java-navigable-set/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>

