<?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/"
	>

<channel>
	<title>opencodez.com</title>
	<atom:link href="http://www.opencodez.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.opencodez.com</link>
	<description>develop . share . reuse</description>
	<pubDate>Sat, 03 Jul 2010 12:12:15 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Java Design Patterns List</title>
		<link>http://www.opencodez.com/java/java-design-patterns-list.htm</link>
		<comments>http://www.opencodez.com/java/java-design-patterns-list.htm#comments</comments>
		<pubDate>Sat, 03 Jul 2010 12:12:15 +0000</pubDate>
		<dc:creator>Pavan</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<category><![CDATA[Design Patterns]]></category>

		<guid isPermaLink="false">http://www.opencodez.com/?p=499</guid>
		<description><![CDATA[Here is the list of Java Design Patterns. I am putting all together for now, will try to add explanation and code samples shortly -
Creational Patterns

Abstract Factory
Builder
Factory
Prototype
Singleton 

Structural Patterns

Adapter
Bridge
Composite
Decorator
Facade
Flyweight
Proxy

Behavioral Patterns

Chain of Responsibility
Command
Interpreter
Iterator
Mediator
Memento
Observer
State
Strategy
Template Method
Visitor

]]></description>
			<content:encoded><![CDATA[<p>Here is the list of Java Design Patterns. I am putting all together for now, will try to add explanation and code samples shortly -</p>
<p><strong>Creational Patterns</strong></p>
<ul>
<li><span style="font-weight: normal;">Abstract Factory</span></li>
<li><span style="font-weight: normal;">Builder</span></li>
<li><span style="font-weight: normal;">Factory</span></li>
<li><span style="font-weight: normal;">Prototype</span></li>
<li><span style="font-weight: normal;">Singleton </span></li>
<p></strong></ul>
<p><strong>Structural Patterns</strong></p>
<ul>
<li>Adapter</li>
<li>Bridge</li>
<li>Composite</li>
<li>Decorator</li>
<li>Facade</li>
<li>Flyweight</li>
<li>Proxy</li>
</ul>
<p><strong>Behavioral Patterns</strong></p>
<ul>
<li>Chain of Responsibility</li>
<li>Command</li>
<li>Interpreter</li>
<li>Iterator</li>
<li>Mediator</li>
<li>Memento</li>
<li>Observer</li>
<li>State</li>
<li>Strategy</li>
<li>Template Method</li>
<li>Visitor</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.opencodez.com/java/java-design-patterns-list.htm/feed</wfw:commentRss>
		</item>
		<item>
		<title>Threads vs. Processes</title>
		<link>http://www.opencodez.com/java/threads-vs-processes.htm</link>
		<comments>http://www.opencodez.com/java/threads-vs-processes.htm#comments</comments>
		<pubDate>Tue, 25 May 2010 09:40:37 +0000</pubDate>
		<dc:creator>Pavan</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<category><![CDATA[process]]></category>

		<category><![CDATA[threads]]></category>

		<guid isPermaLink="false">http://www.opencodez.com/?p=497</guid>
		<description><![CDATA[Threads and Processes helps in concurrent execution of any application.
Process:

an architectural construct : can affect the architecture of an application.
processes are independend execution units
they have their own state, address space
use interprocess communication to communicate between threads

Threads:

coding construct : doesn&#8217;t affect the architecture.  
a single process might contain multiple threds
all threds within a process share [...]]]></description>
			<content:encoded><![CDATA[<p>Threads and Processes helps in concurrent execution of any application.</p>
<p><strong>Process:</strong></p>
<ul>
<li>an architectural construct : can affect the architecture of an application.</li>
<li>processes are independend execution units</li>
<li>they have their own state, address space</li>
<li>use interprocess communication to communicate between threads</li>
</ul>
<p><strong>Threads:</strong></p>
<ul>
<li><span>coding construct : doesn&#8217;t affect the architecture.</span> <span> </span></li>
<li>a single process might contain multiple threds</li>
<li>all threds within a process share process&#8217;s address space and state</li>
<li>threads can communicate with each other directly</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.opencodez.com/java/threads-vs-processes.htm/feed</wfw:commentRss>
		</item>
		<item>
		<title>Core Java Interview Questions - I</title>
		<link>http://www.opencodez.com/java/core-java-interview-questions-i.htm</link>
		<comments>http://www.opencodez.com/java/core-java-interview-questions-i.htm#comments</comments>
		<pubDate>Mon, 24 May 2010 19:16:12 +0000</pubDate>
		<dc:creator>Pavan</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<category><![CDATA[core java]]></category>

		<category><![CDATA[interview]]></category>

		<guid isPermaLink="false">http://www.opencodez.com/?p=482</guid>
		<description><![CDATA[Q. What is System.out.println?

System = final System class
out          =  static PrintStream object
println = method of a printstrem class

Q. What is difference between abstract class and interfaces.

abstract class can provide implementation, interface has no implementation at all
used using extends, used using implements
both can not be instantiated


Q. What will be the output of [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Q. What is System.out.println?</strong></p>
<ul>
<li>System = final System class</li>
<li>out          =  static PrintStream object</li>
<li>println = method of a printstrem class</li>
</ul>
<p><strong>Q. What is difference between abstract class and interfaces.</strong></p>
<ul>
<li>abstract class can provide implementation, interface has no implementation at all</li>
<li>used using extends, used using implements</li>
<li>both can not be instantiated</li>
</ul>
<p><span id="more-482"></span><br />
<strong>Q. What will be the output of following code snippet?</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyTests
<span style="color: #009900;">&#123;</span>
	<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> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">String</span> s<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">null</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>s.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The program will be compiled without any errors, at run time it will throw NullPointerException.<br />
<strong> </strong></p>
<p><strong>Q. What will be the output of following code snippet.</strong></p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyTests
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</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> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The program will be compiled without any errors, at run time it will give an Error saying &#8220;Main Method not public&#8221;<br />
<strong> </strong></p>
<p><strong>Q. What is difference between sleep() and wait()?</strong></p>
<p><strong><span style="font-weight: normal;">When we say sleep(2000), then the thread sleeps for exact 2 seconds, but when we say wait(2000), the  thread might wake up on receiving notify() OR notifyall() call.</span></strong></p>
<p><strong>Q. What are serializable, externalizable interfaces?</strong></p>
<p>These are interfaces used while serialization process. Serialization is a process in which object is converted in byte stream to transfer over network. Serializable acts as marker interface and externalizable is specialization of serializable interface with methods like readExternal, writerExternal, which gives you more control over serialization process.</p>
<p><strong>Q. How threds are created?</strong></p>
<p>In java threads can be created either by extending Thread class Or implementng Runnable Interface.We need to override run() method.</p>
<p><strong>Q. Differenes between Vector and ArrayLists</strong></p>
<ul>
<li>ArrayLists are not synchronized while vectors are synchronized.</li>
<li>By default ArrayList grows by 50%  and Vector grows by 100%.</li>
</ul>
<p><strong>Q. What is difference between HashMap and HashTable</strong></p>
<ul>
<li>HashTable is synchronized and HashMaps are unsynchronizd</li>
<li>HashTable do not allow null as a key, HashMap allow null as key</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.opencodez.com/java/core-java-interview-questions-i.htm/feed</wfw:commentRss>
		</item>
		<item>
		<title>Object oriented concepts - Java</title>
		<link>http://www.opencodez.com/java/polymorphism-in-java.htm</link>
		<comments>http://www.opencodez.com/java/polymorphism-in-java.htm#comments</comments>
		<pubDate>Mon, 24 May 2010 18:30:23 +0000</pubDate>
		<dc:creator>Pavan</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<category><![CDATA[encapsulation]]></category>

		<category><![CDATA[inheritance]]></category>

		<category><![CDATA[oops]]></category>

		<category><![CDATA[polymorphism]]></category>

		<category><![CDATA[runtime polymorphism]]></category>

		<guid isPermaLink="false">http://www.opencodez.com/?p=457</guid>
		<description><![CDATA[Q: What are basic OOPS Concepts?
A: - Inheritance : When a child class acquires all the methods and properties of parent class it is called inheritance. Java supports inheritance either using classes or interfaces. 
Encapsulation: In above class, we have declared id as private and defined two methods that will act as mutator and accessors for this [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Q: What are basic OOPS Concepts?</strong></p>
<p><strong>A: <em><span style="font-weight: normal;">- <strong>Inheritance : </strong><span style="font-style: normal;">When a child class acquires all the methods and properties of parent class it is called inheritance. Java supports inheritance either using classes or interfaces. </span></span></em></strong></p>
<p><span style="font-weight: normal;"><span style="font-style: normal;"><strong><em>Encapsulation: <span style="font-weight: normal;"><span style="font-style: normal;">In above class, we have declared id as private and defined two methods that will act as mutator and accessors for this id propert. Thus we are encapsulateing objects property and  methods with in an object, keeping it safe from outside world.</span></span></em></strong></span></span></p>
<p><span style="font-weight: normal;"><span style="font-style: normal;"><span><strong><em>Polymorphism: <span style="font-weight: normal;"><span style="font-style: normal;">In below scenario, we have added 2 declaration of a function add() to child class, one is for adding  integers and another is to add float values. This kind of behavior when same name is used to perform different tasks as per the parameters passed to it is called polymorphism.</span></span></em></strong></span></span></span></p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Parent
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> id;
	<span style="color: #000000; font-weight: bold;">public</span> Parent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	   aMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> aMethod<span style="color: #009900;">&#40;</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><span style="color: #0000ff;">&quot;I am in Parent&quot;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	  <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> id<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	  <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Child <span style="color: #000000; font-weight: bold;">extends</span> Parent
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> Child<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	  bMethod<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>     	
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> bMethod<span style="color: #009900;">&#40;</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><span style="color: #0000ff;">&quot;I am in Child&quot;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> add<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> a, <span style="color: #000066; font-weight: bold;">int</span> b<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> add<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span> a, <span style="color: #000066; font-weight: bold;">float</span> b<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In java polymorphism is achieved by function overloading. It is also called <strong><em>Compile time Polymorphism. </em><span style="font-weight: normal;">Compile decides at compile time that which version of function is called depending up on the parameters passed.</span></strong></p>
<p><strong><span style="font-weight: normal;">function overriding is also type of polymorphism, some times it is called </span><em>Run time polymorphism</em><span style="font-weight: normal;">, because the methods version is decided depending on the reference type of object at run time.</span></strong></p>
<p><strong></strong></p>
<p><strong><strong>Important Notes:</strong></strong></p>
<p><strong></p>
<ul>
<li><span style="font-weight: normal;">return type is not part of method signature, so mere change of return types will not qualify a method to be polymorphic.</span></li>
<li><span style="font-weight: normal;">if only parameter names are changed then that is not polymorphism.</span></li>
</ul>
<p></strong></p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> aMethod<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> a, <span style="color: #000066; font-weight: bold;">long</span> b<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><span style="color: #0000ff;">&quot;int a, long b&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> aMethod<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span> a, <span style="color: #000066; font-weight: bold;">int</span> b<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><span style="color: #0000ff;">&quot;long a, int b&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For above methods to be polymorphic you need to use them as -</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">someobject.<span style="color: #006633;">aMethod</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>,2L<span style="color: #009900;">&#41;</span>;
someobject.<span style="color: #006633;">aMethod</span><span style="color: #009900;">&#40;</span>1L,<span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span>;
<span style="color: #666666; font-style: italic;">//someobject.aMethod(1,2); //compile time error</span>
<span style="color: #666666; font-style: italic;">//someobject.aMethod(2,1); //compile time error</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.opencodez.com/java/polymorphism-in-java.htm/feed</wfw:commentRss>
		</item>
		<item>
		<title>Java Sorting using Comparator</title>
		<link>http://www.opencodez.com/java/java-sorting-using-comparator.htm</link>
		<comments>http://www.opencodez.com/java/java-sorting-using-comparator.htm#comments</comments>
		<pubDate>Mon, 24 May 2010 11:01:16 +0000</pubDate>
		<dc:creator>Pavan</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<category><![CDATA[comparator]]></category>

		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://www.opencodez.com/?p=449</guid>
		<description><![CDATA[Lets see how we can sort our Employee objects using different criteria like id, name, salary.
SortTest remains the main class that will perform creating and filling objects for us. This time employee class is plain old java classs that holds some data about each employee we create.
Here I have created scenarios or you can say strategies [...]]]></description>
			<content:encoded><![CDATA[<p>Lets see how we can sort our Employee objects using different criteria like id, name, salary.</p>
<p>SortTest remains the main class that will perform creating and filling objects for us. This time employee class is plain old java classs that holds some data about each employee we create.</p>
<p>Here I have created scenarios or you can say strategies OR classes for sorting as SortById,  SortByName, SortBySalary. Each of above class implements comparator interface and overrides compare method, which takes two objects to compare and return eithre -1, 1 or 0.</p>
<p>Note: I have added few helper functions that really helps while coding like  <strong><em>print</em></strong> - this takes array list of objects and prints in format I want instead of class id and all that.<strong><em> toString</em></strong> -  overridden this function so I can return the class string as required</p>
<p>Here goes the code -<br />
<span id="more-449"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SortTest
<span style="color: #009900;">&#123;</span>
	<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> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">new</span> SortTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> SortTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//fill some employee objects</span>
		<span style="color: #003399;">ArrayList</span> list <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
		list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span>,<span style="color: #0000ff;">&quot;Shifoo&quot;</span>,<span style="color: #cc66cc;">150000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">504</span>,<span style="color: #0000ff;">&quot;Oogway&quot;</span>,<span style="color: #cc66cc;">120000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">503</span>,<span style="color: #0000ff;">&quot;Tigress&quot;</span>,<span style="color: #cc66cc;">100000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">730</span>,<span style="color: #0000ff;">&quot;Mantis&quot;</span>,<span style="color: #cc66cc;">45000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<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;Initial List :&quot;</span><span style="color: #009900;">&#41;</span>;
		print<span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</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;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #003399;">Collections</span>.<span style="color: #006633;">sort</span><span style="color: #009900;">&#40;</span>list,<span style="color: #000000; font-weight: bold;">new</span> SortyById<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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;Sorted List By Id:&quot;</span><span style="color: #009900;">&#41;</span>;
		print<span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</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;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #003399;">Collections</span>.<span style="color: #006633;">sort</span><span style="color: #009900;">&#40;</span>list,<span style="color: #000000; font-weight: bold;">new</span> SortyByName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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;Sorted List By Name:&quot;</span><span style="color: #009900;">&#41;</span>;
		print<span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</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;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #003399;">Collections</span>.<span style="color: #006633;">sort</span><span style="color: #009900;">&#40;</span>list,<span style="color: #000000; font-weight: bold;">new</span> SortyBySalary<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</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;Sorted List By Salary:&quot;</span><span style="color: #009900;">&#41;</span>;
		print<span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</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;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> print<span style="color: #009900;">&#40;</span><span style="color: #003399;">ArrayList</span> list<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Iterator</span> it <span style="color: #339933;">=</span> list.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>it.<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>
			Employee emp <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Employee<span style="color: #009900;">&#41;</span> it.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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>emp<span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Employee
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> id;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> name;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> salary;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> id, <span style="color: #003399;">String</span> name,<span style="color: #000066; font-weight: bold;">double</span> salary <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id;
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name;
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">salary</span> <span style="color: #339933;">=</span> salary;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #339933;">+</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #339933;">+</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">salary</span>;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//scenario | strategy - I</span>
<span style="color: #000000; font-weight: bold;">class</span> SortyById <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Comparator</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> compare<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> object1, <span style="color: #003399;">Object</span> object2<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #339933;">=</span>0;
&nbsp;
		Employee emp1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Employee<span style="color: #009900;">&#41;</span> object1;
		Employee emp2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Employee<span style="color: #009900;">&#41;</span> object2;
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>emp1.<span style="color: #006633;">id</span> <span style="color: #339933;">&amp;</span>gt; emp2.<span style="color: #006633;">id</span><span style="color: #009900;">&#41;</span>
			value <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>;
		<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>emp1.<span style="color: #006633;">id</span> <span style="color: #339933;">&amp;</span>lt; emp2.<span style="color: #006633;">id</span><span style="color: #009900;">&#41;</span>
			value <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span>;
		<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>emp1.<span style="color: #006633;">id</span> <span style="color: #339933;">==</span> emp2.<span style="color: #006633;">id</span><span style="color: #009900;">&#41;</span>
			value <span style="color: #339933;">=</span> 0;
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> value;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//scenario | strategy - II</span>
<span style="color: #000000; font-weight: bold;">class</span> SortyByName <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Comparator</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> compare<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> object1, <span style="color: #003399;">Object</span> object2<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		Employee emp1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Employee<span style="color: #009900;">&#41;</span> object1;
		Employee emp2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Employee<span style="color: #009900;">&#41;</span> object2;
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> emp1.<span style="color: #006633;">name</span>.<span style="color: #006633;">compareTo</span><span style="color: #009900;">&#40;</span>emp2.<span style="color: #006633;">name</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//scenario | strategy - III</span>
<span style="color: #000000; font-weight: bold;">class</span> SortyBySalary <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Comparator</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> compare<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> object1, <span style="color: #003399;">Object</span> object2<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #339933;">=</span>0;
&nbsp;
		Employee emp1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Employee<span style="color: #009900;">&#41;</span> object1;
		Employee emp2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Employee<span style="color: #009900;">&#41;</span> object2;
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>emp1.<span style="color: #006633;">salary</span> <span style="color: #339933;">&amp;</span>gt; emp2.<span style="color: #006633;">salary</span><span style="color: #009900;">&#41;</span>
			value <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>;
		<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>emp1.<span style="color: #006633;">salary</span> <span style="color: #339933;">&amp;</span>lt; emp2.<span style="color: #006633;">salary</span><span style="color: #009900;">&#41;</span>
			value <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span>;
		<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>emp1.<span style="color: #006633;">salary</span> <span style="color: #339933;">==</span> emp2.<span style="color: #006633;">salary</span><span style="color: #009900;">&#41;</span>
			value <span style="color: #339933;">=</span> 0;
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> value;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Output:</strong></p>
<p style="text-align: center;"><strong><a href="http://www.opencodez.com/wp-content/uploads/2010/05/sort_comparator.png"><img class="aligncenter size-full wp-image-450" title="sort_comparator" src="http://www.opencodez.com/wp-content/uploads/2010/05/sort_comparator.png" alt="sort_comparator" width="596" height="342" /></a></strong></p>
<p>As you can see -</p>
<ul>
<li>we can create multiple sort strategy for sorting</li>
<li>we can compare heterogeneous objects</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.opencodez.com/java/java-sorting-using-comparator.htm/feed</wfw:commentRss>
		</item>
		<item>
		<title>Java Sorting using Comparable</title>
		<link>http://www.opencodez.com/java/java-sorting-using-comparable.htm</link>
		<comments>http://www.opencodez.com/java/java-sorting-using-comparable.htm#comments</comments>
		<pubDate>Mon, 24 May 2010 10:27:01 +0000</pubDate>
		<dc:creator>Pavan</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<category><![CDATA[comparable]]></category>

		<category><![CDATA[comparator]]></category>

		<category><![CDATA[java generics]]></category>

		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://www.opencodez.com/?p=426</guid>
		<description><![CDATA[In Java you can do sorting by implementing 2 interfaces
 1) Comparable
2) Comparator
Here we will see sorting example using Comparable Interface. As an example we will be sorting objects of Employee class.
When using comaprable interface make sure the object class
which you are sorting implements comaprable interface and override compareTo method correctly.
As I was more used [...]]]></description>
			<content:encoded><![CDATA[<p>In Java you can do sorting by implementing 2 interfaces<br />
<strong> 1) Comparable<br />
2) Comparator</strong></p>
<p>Here we will see sorting example using Comparable Interface. As an example we will be sorting objects of Employee class.</p>
<p>When using comaprable interface make sure the object class<br />
which you are sorting implements comaprable interface and override compareTo method correctly.</p>
<p>As I was more used to Java 1.4, I didnt followed any <a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html">Java Generics</a> implemetation.</p>
<p>SortTest is main class that will actually creaate an array list and fill it with some random Employee objects.</p>
<p>Employee is class that will implement  compareTo method. I have added simple if-else control structure that will decide what to return on the basis of employee id.<br />
less : -1           equal : 0           greater : 1</p>
<p>Here is the code for classes:<br />
<span id="more-426"></span></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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
</pre></td><td class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SortTest
<span style="color: #009900;">&#123;</span>
	<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> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">new</span> SortTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> SortTest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//fill some employee objects</span>
		<span style="color: #003399;">ArrayList</span> list <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ArrayList</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
		list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">500</span>,<span style="color: #0000ff;">&quot;Shifoo&quot;</span>,<span style="color: #cc66cc;">150000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">504</span>,<span style="color: #0000ff;">&quot;Oogway&quot;</span>,<span style="color: #cc66cc;">120000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">503</span>,<span style="color: #0000ff;">&quot;Tigress&quot;</span>,<span style="color: #cc66cc;">100000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">130</span>,<span style="color: #0000ff;">&quot;Mantis&quot;</span>,<span style="color: #cc66cc;">100000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<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;Initial List :&quot;</span><span style="color: #009900;">&#41;</span>;
		print<span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</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;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #003399;">Collections</span>.<span style="color: #006633;">sort</span><span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</span>;
&nbsp;
		<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;Sorted List :&quot;</span><span style="color: #009900;">&#41;</span>;
		print<span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</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;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> print<span style="color: #009900;">&#40;</span><span style="color: #003399;">ArrayList</span> list<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Iterator</span> it <span style="color: #339933;">=</span> list.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">while</span><span style="color: #009900;">&#40;</span>it.<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>
			Employee emp <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Employee<span style="color: #009900;">&#41;</span> it.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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>emp<span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> Employee <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Comparable</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> id;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> name;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">double</span> salary;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Employee<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> id, <span style="color: #003399;">String</span> name,<span style="color: #000066; font-weight: bold;">double</span> salary <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">=</span> id;
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span> <span style="color: #339933;">=</span> name;
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">salary</span> <span style="color: #339933;">=</span> salary;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> compareTo<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> object<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #339933;">=</span>0;
&nbsp;
		Employee emp <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Employee<span style="color: #009900;">&#41;</span> object;
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">&amp;</span>gt; emp.<span style="color: #006633;">id</span><span style="color: #009900;">&#41;</span>
			value <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>;
		<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">&amp;</span>lt; emp.<span style="color: #006633;">id</span><span style="color: #009900;">&#41;</span>
			value <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span>;
		<span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">==</span> emp.<span style="color: #006633;">id</span><span style="color: #009900;">&#41;</span>
			value <span style="color: #339933;">=</span> 0;
&nbsp;
		<span style="color: #000000; font-weight: bold;">return</span> value;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #339933;">+</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">name</span><span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #339933;">+</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">salary</span>;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Output:</strong><br />
<a href="http://www.opencodez.com/wp-content/uploads/2010/05/sort_comparable.png"><img class="aligncenter size-full wp-image-438" title="sort_comparable" src="http://www.opencodez.com/wp-content/uploads/2010/05/sort_comparable.png" alt="sort_comparable" width="668" height="331" /></a><br />
<strong> Limitations:</strong></p>
<ol>
<li>Objects should be mutally comparable. i.e you can not compare objects of different classes.</li>
<li>Using comparable binds sorting to one perticular strategy.<br />e.g. In above example we have sorted using employee id to compare. now if we need to sort on employee name it will be difficult.</li>
</ol>
<p>Next, I will be putting similar example using Comparator Interface.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencodez.com/java/java-sorting-using-comparable.htm/feed</wfw:commentRss>
		</item>
		<item>
		<title>Get all urls from a web page</title>
		<link>http://www.opencodez.com/java/get-all-urls-from-a-web-page.htm</link>
		<comments>http://www.opencodez.com/java/get-all-urls-from-a-web-page.htm#comments</comments>
		<pubDate>Sat, 11 Jul 2009 07:50:57 +0000</pubDate>
		<dc:creator>Pavan</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<category><![CDATA[crawler]]></category>

		<category><![CDATA[url]]></category>

		<guid isPermaLink="false">http://www.opencodez.com/?p=409</guid>
		<description><![CDATA[Here I&#8217;m going to post a class that will extract all valid urls from a web page. My class uses &#8220;URLConnectionReader&#8221; provided by Sun Tutorial
Class defines 2 constructors. 

One by default returns you the vector containing only text/html url objects  from page. 
For the other you can specify the type of urls you want [...]]]></description>
			<content:encoded><![CDATA[<p>Here I&#8217;m going to post a class that will extract all valid urls from a web page. My class uses &#8220;URLConnectionReader&#8221; provided by <a href="http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html">Sun Tutorial</a></p>
<p>Class defines 2 constructors. </p>
<ol>
<li>One by default returns you the vector containing only text/html url objects  from page. </li>
<li>For the other you can specify the type of urls you want from a page. This is helpful when you want to get all images, videos or any other media urls.
</li>
</ol>
<p>The class also considers relative urls. It returns relative urls with http and host name prefixed.<br />
E.g. If you have urls like &#8220;/about.php&#8221;, then class will return &#8220;http://hostname.domain/about.php&#8221;<br />
<span id="more-409"></span><br />
<strong>The URLFinder</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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
</pre></td><td class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.net.*</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.*</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> URLFinder 
<span style="color: #009900;">&#123;</span>
<span style="color: #003399;">String</span> content;
<span style="color: #003399;">Vector</span> urlVector;
<span style="color: #003399;">String</span> type;
<span style="color: #003399;">URL</span> tUrl;
URLConnectionReader ucreader;
<span style="color: #003399;">String</span> host;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> URLFinder<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> uri<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 ucreader <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> URLConnectionReader<span style="color: #009900;">&#40;</span>uri<span style="color: #009900;">&#41;</span>;		
 <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">content</span> <span style="color: #339933;">=</span> ucreader.<span style="color: #006633;">content</span>.<span style="color: #006633;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
 <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">host</span> <span style="color: #339933;">=</span> ucreader.<span style="color: #006633;">host</span>;
&nbsp;
 urlVector <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Vector</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">30</span>,<span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span>;
 <span style="color: #000066; font-weight: bold;">int</span> index <span style="color: #339933;">=</span> 0;
 <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>index <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">content</span>.<span style="color: #006633;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;a&quot;</span>, index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>index <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">content</span>.<span style="color: #006633;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;href&quot;</span>, index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">break</span>;
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>index <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">content</span>.<span style="color: #006633;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;=&quot;</span>, index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">break</span>;
	index++;
	<span style="color: #003399;">String</span> remaining <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">content</span>.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span>;
	<span style="color: #003399;">StringTokenizer</span> st <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringTokenizer</span><span style="color: #009900;">&#40;</span>remaining, <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\'</span>&gt;#&quot;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #003399;">String</span> strLink <span style="color: #339933;">=</span> st.<span style="color: #006633;">nextToken</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
	tUrl <span style="color: #339933;">=</span> isValidUrl<span style="color: #009900;">&#40;</span>strLink<span style="color: #009900;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">try</span> 
	<span style="color: #009900;">&#123;</span> 
		<span style="color: #003399;">URLConnection</span> urlConnection <span style="color: #339933;">=</span> tUrl.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;				
		<span style="color: #003399;">String</span> type <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span>urlConnection.<span style="color: #006633;">getContentType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;				
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>type <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</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><span style="color: #0000ff;">&quot;ERROR: invalid type &quot;</span>
			<span style="color: #339933;">+</span>strLink<span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=&quot;</span> <span style="color: #339933;">+</span> type<span style="color: #009900;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">continue</span>;
		<span style="color: #009900;">&#125;</span>	
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>type.<span style="color: #006633;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ext/html&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span>0<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><span style="color: #0000ff;">&quot;ERROR: invalid type &quot;</span>
			<span style="color: #339933;">+</span>strLink<span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=&quot;</span> <span style="color: #339933;">+</span> type<span style="color: #009900;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">continue</span>;
		<span style="color: #009900;">&#125;</span>
		urlVector.<span style="color: #006633;">addElement</span><span style="color: #009900;">&#40;</span>tUrl<span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span> 
	<span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<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><span style="color: #0000ff;">&quot;ERROR: invalid URL &quot;</span> 
			<span style="color: #339933;">+</span> strLink<span style="color: #009900;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">continue</span>;
	<span style="color: #009900;">&#125;</span>			
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> URLFinder<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> data, <span style="color: #003399;">String</span> type<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
 <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">content</span> <span style="color: #339933;">=</span> data.<span style="color: #006633;">toLowerCase</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
 urlVector <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Vector</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">30</span>,<span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span>;
 <span style="color: #000066; font-weight: bold;">int</span> index <span style="color: #339933;">=</span> 0;
 <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>index <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">content</span>.<span style="color: #006633;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;a&quot;</span>, index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
 <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>index <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">content</span>.<span style="color: #006633;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;href&quot;</span>, index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">break</span>;
	<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>index <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">content</span>.<span style="color: #006633;">indexOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;=&quot;</span>, index<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">break</span>;
	index++;
	<span style="color: #003399;">String</span> remaining <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">content</span>.<span style="color: #006633;">substring</span><span style="color: #009900;">&#40;</span>index<span style="color: #009900;">&#41;</span>;
	<span style="color: #003399;">StringTokenizer</span> st <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">StringTokenizer</span><span style="color: #009900;">&#40;</span>remaining, <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\'</span>&gt;#&quot;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #003399;">String</span> strLink <span style="color: #339933;">=</span> st.<span style="color: #006633;">nextToken</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;		
&nbsp;
	tUrl <span style="color: #339933;">=</span> isValidUrl<span style="color: #009900;">&#40;</span>strLink<span style="color: #009900;">&#41;</span>;		    
	<span style="color: #000000; font-weight: bold;">try</span> 
	<span style="color: #009900;">&#123;</span> 
		<span style="color: #003399;">URLConnection</span> urlConnection <span style="color: #339933;">=</span> tUrl.<span style="color: #006633;">openConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;				
		<span style="color: #003399;">String</span> utype <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#41;</span>urlConnection.<span style="color: #006633;">getContentType</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>utype <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</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><span style="color: #0000ff;">&quot;ERROR: invalid type &quot;</span>
			<span style="color: #339933;">+</span>strLink<span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=&quot;</span> <span style="color: #339933;">+</span> utype<span style="color: #009900;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">continue</span>;
		<span style="color: #009900;">&#125;</span>	
		<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>utype.<span style="color: #006633;">indexOf</span><span style="color: #009900;">&#40;</span>type<span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span>0<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><span style="color: #0000ff;">&quot;ERROR: invalid type &quot;</span>
			<span style="color: #339933;">+</span>strLink<span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;=&quot;</span> <span style="color: #339933;">+</span> utype<span style="color: #009900;">&#41;</span>;
			<span style="color: #000000; font-weight: bold;">continue</span>;
		<span style="color: #009900;">&#125;</span>
		urlVector.<span style="color: #006633;">addElement</span><span style="color: #009900;">&#40;</span>tUrl.<span style="color: #006633;">toURI</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span> 
	<span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<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><span style="color: #0000ff;">&quot;ERROR: invalid URL &quot;</span> 
			<span style="color: #339933;">+</span> strLink<span style="color: #009900;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">continue</span>;
	<span style="color: #009900;">&#125;</span>			
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">URL</span> isValidUrl<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> strLink<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">try</span>	
	<span style="color: #009900;">&#123;</span>
		tUrl <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span>strLink<span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">MalformedURLException</span> e<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		strLink <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://&quot;</span><span style="color: #339933;">+</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">host</span><span style="color: #339933;">+</span>strLink;
		tUrl <span style="color: #339933;">=</span> isValidUrl<span style="color: #009900;">&#40;</span>strLink<span style="color: #009900;">&#41;</span>;		
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000000; font-weight: bold;">return</span> tUrl;
<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Usage</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="java java" style="font-family:monospace;">finder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> URLFinder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.mydomain.com/&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000066; font-weight: bold;">int</span> cap <span style="color: #339933;">=</span> finder.<span style="color: #006633;">urlVector</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;			 
<span style="color: #003399;">String</span> buf <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span>;			 
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> j<span style="color: #339933;">=</span>0;j<span style="color: #339933;">&lt;</span>cap;j<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>				 
 <span style="color: #003399;">URL</span> tmp <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">URL</span><span style="color: #009900;">&#41;</span>finder.<span style="color: #006633;">urlVector</span>.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>j<span style="color: #009900;">&#41;</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>tmp.<span style="color: #006633;">toURI</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This will get you all the urls from any web page.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencodez.com/java/get-all-urls-from-a-web-page.htm/feed</wfw:commentRss>
		</item>
		<item>
		<title>Read Config File in Java</title>
		<link>http://www.opencodez.com/java/read-config-file-in-java.htm</link>
		<comments>http://www.opencodez.com/java/read-config-file-in-java.htm#comments</comments>
		<pubDate>Sat, 11 Jul 2009 07:22:12 +0000</pubDate>
		<dc:creator>Pavan</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<category><![CDATA[config]]></category>

		<guid isPermaLink="false">http://www.opencodez.com/?p=400</guid>
		<description><![CDATA[In my last post I mentioned &#8220;Config Class&#8221;couple of times.. here you will see actual class and its usage. The class is very simple. We just need to provide proper path to config file we going to use.
The Config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.*;
import java.util.Properties;
&#160;
public class Config 
&#123;
   Properties configFile;
   public Config&#40;&#41;
   &#123;
	configFile [...]]]></description>
			<content:encoded><![CDATA[<p>In my last post I mentioned &#8220;Config Class&#8221;couple of times.. here you will see actual class and its usage. The class is very simple. We just need to provide proper path to config file we going to use.</p>
<p><strong>The Config</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
23
</pre></td><td class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.*</span>;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Properties</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Config 
<span style="color: #009900;">&#123;</span>
   <span style="color: #003399;">Properties</span> configFile;
   <span style="color: #000000; font-weight: bold;">public</span> Config<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
	configFile <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> java.<span style="color: #006633;">util</span>.<span style="color: #003399;">Properties</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>			
	  configFile.<span style="color: #006633;">load</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getClassLoader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.
	  <span style="color: #006633;">getResourceAsStream</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;myapp/config.cfg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;			
	<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> eta<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	    eta.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
	<span style="color: #009900;">&#125;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getProperty<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">String</span> value <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">configFile</span>.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span>;		
	<span style="color: #000000; font-weight: bold;">return</span> value;
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p> You can get any property/settings from config with method &#8216;getProperty&#8217;</p>
<p>Here is my config file</p>

<div class="wp_syntax"><div class="code"><pre class="java java" style="font-family:monospace;">#This is comment
mDbUser <span style="color: #339933;">=</span> myuser
mDbHost <span style="color: #339933;">=</span> myserver
mDbPwds <span style="color: #339933;">=</span> mypwd
mDbName <span style="color: #339933;">=</span> mydb</pre></div></div>

<p>Usage:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="java java" style="font-family:monospace;">cfg <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Config<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
dbname   <span style="color: #339933;">=</span> cfg.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mDbUser&quot;</span><span style="color: #009900;">&#41;</span>;</pre></td></tr></table></div>

<p>Hope you find this useful..</p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencodez.com/java/read-config-file-in-java.htm/feed</wfw:commentRss>
		</item>
		<item>
		<title>Connect to MySQL in Java</title>
		<link>http://www.opencodez.com/java/connect-to-mysql-in-java.htm</link>
		<comments>http://www.opencodez.com/java/connect-to-mysql-in-java.htm#comments</comments>
		<pubDate>Tue, 07 Jul 2009 16:56:48 +0000</pubDate>
		<dc:creator>Pavan</dc:creator>
		
		<category><![CDATA[Java]]></category>

		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.opencodez.com/?p=392</guid>
		<description><![CDATA[It&#8217;s been too long, I didnt get time to post anything OpenSource.
So here I am again with one simple class Database, to connect to MySQL in Java.
You can use this class in 2 ways -

 Specifying user,pwd and database name to connect 
 Specifying connection ID and class will load rest of the variables from [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been too long, I didnt get time to post anything OpenSource.<br />
So here I am again with one simple class Database, to connect to MySQL in Java.<br />
You can use this class in 2 ways -</p>
<ol>
<li> Specifying user,pwd and database name to connect </li>
<li> Specifying connection ID and class will load rest of the variables from CONFIG file</li>
</ol>
<p>Here I will post the Database class only, class to read config file and loading properties will be posted in different post.<br />
<span id="more-392"></span><br />
<strong>The Database </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
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
</pre></td><td class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.*</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Database 
<span style="color: #009900;">&#123;</span>	
	<span style="color: #003399;">Connection</span> conn;
	<span style="color: #003399;">Statement</span> stmt;
	<span style="color: #003399;">PreparedStatement</span> pstmt;
	<span style="color: #003399;">ResultSet</span> rs;
	Config cfg;
	<span style="color: #000066; font-weight: bold;">int</span> affectedRows;
&nbsp;
	<span style="color: #666666; font-style: italic;">/*Reading From CFG*/</span>
	<span style="color: #003399;">String</span> dbuser,dbpwd,dbname,dbserver;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Database<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>		
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Database<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> database, <span style="color: #003399;">String</span> user,<span style="color: #003399;">String</span> pwd<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">affectedRows</span> <span style="color: #339933;">=</span> 0;
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.mysql.jdbc.Driver&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
			conn <span style="color: #339933;">=</span> <span style="color: #003399;">DriverManager</span>.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">&quot;jdbc:mysql://localhost:3306/&quot;</span><span style="color: #339933;">+</span>database, user
			, pwd<span style="color: #009900;">&#41;</span>;
			stmt <span style="color: #339933;">=</span> conn.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;			
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> Database<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> dbid<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		cfg <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Config<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">dbname</span>   <span style="color: #339933;">=</span> cfg.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mStrName&quot;</span><span style="color: #339933;">+</span>dbid<span style="color: #009900;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">dbuser</span>   <span style="color: #339933;">=</span> cfg.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mStrUser&quot;</span><span style="color: #339933;">+</span>dbid<span style="color: #009900;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">dbpwd</span>    <span style="color: #339933;">=</span> cfg.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mStrPwds&quot;</span><span style="color: #339933;">+</span>dbid<span style="color: #009900;">&#41;</span>;
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">dbserver</span> <span style="color: #339933;">=</span> cfg.<span style="color: #006633;">getProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mStrHost&quot;</span><span style="color: #339933;">+</span>dbid<span style="color: #009900;">&#41;</span>;
&nbsp;
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">Class</span>.<span style="color: #006633;">forName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.mysql.jdbc.Driver&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">newInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
			conn <span style="color: #339933;">=</span> <span style="color: #003399;">DriverManager</span>.<span style="color: #006633;">getConnection</span><span style="color: #009900;">&#40;</span> 
			<span style="color: #0000ff;">&quot;jdbc:mysql://localhost:3306/&quot;</span><span style="color: #339933;">+</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">dbname</span>, <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">dbuser</span>
			, <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">dbpwd</span><span style="color: #009900;">&#41;</span>;
			stmt <span style="color: #339933;">=</span> conn.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;			
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">ResultSet</span> query<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> qry<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>		
			rs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">stmt</span>.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span>qry<span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">SQLException</span> se<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><span style="color: #0000ff;">&quot;Error : &quot;</span> <span style="color: #339933;">+</span> se.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> rs;
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> update<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> qry<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">int</span> count <span style="color: #339933;">=</span> 0;
		<span style="color: #000000; font-weight: bold;">try</span><span style="color: #009900;">&#123;</span>
			count <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">stmt</span>.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span>qry<span style="color: #009900;">&#41;</span>; 
		<span style="color: #009900;">&#125;</span><span style="color: #000000; font-weight: bold;">catch</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Exception</span> se<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><span style="color: #0000ff;">&quot;Error : &quot;</span> <span style="color: #339933;">+</span> se.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">return</span> count;
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And here is how we are going to use this class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="java java" style="font-family:monospace;">Database db <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Database<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mydb&quot;</span>,<span style="color: #0000ff;">&quot;myuser&quot;</span>,<span style="color: #0000ff;">&quot;mypwd&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #666666; font-style: italic;">//simple query</span>
<span style="color: #003399;">ResultSet</span> rs <span style="color: #339933;">=</span> db.<span style="color: #006633;">query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM mytable&quot;</span><span style="color: #009900;">&#41;</span>;</pre></td></tr></table></div>

<p>Now you can traverse record rs set in a way you want&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="java java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//update query</span>
<span style="color: #000066; font-weight: bold;">int</span> affected <span style="color: #339933;">=</span> db.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;DELETE FROM mytable&quot;</span><span style="color: #009900;">&#41;</span>;</pre></td></tr></table></div>

<p>We will shortly see how we are going to use Config class and its usage.. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencodez.com/java/connect-to-mysql-in-java.htm/feed</wfw:commentRss>
		</item>
		<item>
		<title>Using .htaccess in XAMPP on Windows</title>
		<link>http://www.opencodez.com/apache/htaccess-in-xampp-windows.htm</link>
		<comments>http://www.opencodez.com/apache/htaccess-in-xampp-windows.htm#comments</comments>
		<pubDate>Fri, 03 Apr 2009 21:24:39 +0000</pubDate>
		<dc:creator>Pavan</dc:creator>
		
		<category><![CDATA[apache]]></category>

		<category><![CDATA[htaccess]]></category>

		<category><![CDATA[mod rewrite]]></category>

		<category><![CDATA[xampp]]></category>

		<guid isPermaLink="false">http://www.opencodez.com/?p=377</guid>
		<description><![CDATA[I googled quiet some time on using custom links for your site on windows using XAMPP. All of them suggested me to uncomment line for ‘mod_rewrite’, set AllowOverride to All. I tried this but it still faced same issue, a 404 error page.
Then I came to one more solution that I tried an bingooo!, it [...]]]></description>
			<content:encoded><![CDATA[<p>I googled quiet some time on using custom links for your site on windows using <strong>XAMPP</strong>. All of them suggested me to uncomment line for ‘mod_rewrite’, set AllowOverride to All. I tried this but it still faced same issue, a 404 error page.</p>
<p>Then I came to one more solution that I tried an bingooo!, it worked like charm. Just to keep note and save some time of other developers , I am putting all steps here . First 2, 3 steps are commonly suggested.<br />
<span id="more-377"></span></p>
<ul>
<li> Open your apache conf file &#8220;httpd.conf&#8221; in any editor of your choice.</li>
</ul>
<ul>
<li> Search &#8220;mod_rewrite.so&#8221;, you may encounter line as follows -<br />
#LoadModule rewrite_module modules/mod_rewrite.so</li>
</ul>
<ul>
<li> Remove the comment &#8220;#&#8221;.</li>
</ul>
<ul>
<li> Find &#8220;AccessFileName&#8221; , you may see a line like -<br />
&#8220;AccessFileName ht.acl&#8221; change it to &#8220;AccessFileName htaccess.txt&#8221;.</li>
</ul>
<ul>
<li> Create &#8220;htaccess.txt&#8221; in sites root directory.</li>
</ul>
<ul>
<li> Restart apache</li>
</ul>
<p>Thats it, you are done!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.opencodez.com/apache/htaccess-in-xampp-windows.htm/feed</wfw:commentRss>
		</item>
	</channel>
</rss>

