<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;C0AMQ3g_fSp7ImA9WhNRE0s.&quot;"><id>tag:blogger.com,1999:blog-2138644806777257073</id><updated>2012-11-08T10:03:02.645+02:00</updated><category term="junit" /><category term="games" /><category term="datastructure" /><category term="java" /><title>Code How Tos</title><subtitle type="html" /><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://codehowtos.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://codehowtos.blogspot.com/" /><author><name>Igal Kreichman</name><uri>https://plus.google.com/108062574077972170259</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-jAR-7NAroHQ/AAAAAAAAAAI/AAAAAAAAFmc/_csjQQoLFfU/s512-c/photo.jpg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/CodeHowTos" /><feedburner:info uri="codehowtos" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;AkIDQ3s4fSp7ImA9WhRTGUw.&quot;"><id>tag:blogger.com,1999:blog-2138644806777257073.post-1651382192248556313</id><published>2011-11-10T11:49:00.000+02:00</published><updated>2011-11-10T11:49:32.535+02:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-11-10T11:49:32.535+02:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="java" /><title>Introduction to LambdaJ: Get rid of those pesky loops</title><content type="html">After a very long pause, I thought it'd be nice to introduce some cool Java technologies I stumbled upon lately.&lt;br /&gt;
&lt;br /&gt;
So, straight to business, I want to talk a bit about &lt;a href="http://code.google.com/p/lambdaj/"&gt;LambdaJ&lt;/a&gt;. It's a super cool infrastructure which is targeting those loops you always end up writing in Java.&lt;br /&gt;
&lt;br /&gt;
Let's see some examples.&lt;br /&gt;
Say you've got an address book, and it's saved in a List&amp;lt;Person&amp;gt; where Person has a getSurname() method.&lt;br /&gt;
We wish to get all those Smiths among the members of the address book.&lt;br /&gt;
Now, usually you find yourself writing something like:&lt;br /&gt;
&lt;pre class="java" name="code"&gt;List&amp;lt;Person&amp;gt; smiths = new ArrayList&amp;lt;person&amp;gt;();
for (Person person : addressBook) {
    if (person.getSurname().equals("Smith")) {
        smiths.add(person);
    }
}
&lt;/pre&gt;
&lt;br /&gt;
Instead, we like this clean Python syntax:&lt;br /&gt;
&lt;pre class="python" name="code"&gt;smiths = [person for person in addressBook if person.getSurname() == "Smith"]
&lt;/pre&gt;
&lt;br /&gt;
So, Java is not the most flexible language there is, but this LambdaJ syntax really nears perfection.&lt;br /&gt;
This will look like:&lt;br /&gt;
&lt;pre class="java" name="code"&gt;List&amp;lt;Person&amp;gt; smiths = select(addressBook, having(on(Person.class).getSurname().equals("Smith")));
&lt;/pre&gt;
&lt;br /&gt;
Ok, that was nice. Now let's look at a foreach block.&lt;br /&gt;
Regularly, we'd see something like:&lt;br /&gt;
&lt;pre class="java" name="code"&gt;for (Person knight : newKnights) {
    knight.setTitle("Sir");
}
&lt;/pre&gt;
&lt;br /&gt;
We could do this instead:&lt;br /&gt;
&lt;pre class="java" name="code"&gt;forEach(newKnights).setTitle("Sir");
&lt;/pre&gt;
&lt;br /&gt;
Finally, lets sort. I won't even write the code that sorts the address book by first name without LambdaJ.&lt;br /&gt;
With LambdaJ it looks really cool:&lt;br /&gt;
&lt;pre class="java" name="code"&gt;sort(addressBook, on(Person.class).getFirstName());
&lt;/pre&gt;
&lt;br /&gt;
So, many thanks to the developers who wrote this project, and for making it open source.&lt;br /&gt;
That's all for now, thanks for reading.&lt;img src="http://feeds.feedburner.com/~r/CodeHowTos/~4/d9KgaZJ2A0E" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://codehowtos.blogspot.com/feeds/1651382192248556313/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://codehowtos.blogspot.com/2011/11/introduction-to-lambdaj-get-rid-of.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2138644806777257073/posts/default/1651382192248556313?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2138644806777257073/posts/default/1651382192248556313?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeHowTos/~3/d9KgaZJ2A0E/introduction-to-lambdaj-get-rid-of.html" title="Introduction to LambdaJ: Get rid of those pesky loops" /><author><name>Igal Kreichman</name><uri>https://plus.google.com/108062574077972170259</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-jAR-7NAroHQ/AAAAAAAAAAI/AAAAAAAAFmc/_csjQQoLFfU/s512-c/photo.jpg" /></author><thr:total>0</thr:total><feedburner:origLink>http://codehowtos.blogspot.com/2011/11/introduction-to-lambdaj-get-rid-of.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUUFQng_fip7ImA9WhZSGEg.&quot;"><id>tag:blogger.com,1999:blog-2138644806777257073.post-5613906235032085506</id><published>2011-04-03T20:51:00.002+03:00</published><updated>2011-04-03T20:53:33.646+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-03T20:53:33.646+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="junit" /><title>Run a JUnit test repeatedly</title><content type="html">&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;I like JUnit. It's easy to extend, and easy to use.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;After reading abyx's post on&amp;nbsp;&lt;/span&gt;&lt;a href="https://gist.github.com/897229"&gt;&lt;span class="Apple-style-span" style="color: #6fa8dc; font-family: Arial, Helvetica, sans-serif;"&gt;Retry Rule&lt;/span&gt;&lt;/a&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;, I decided to write a post about running repeating tests with JUnit.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;Hope you enjoy this:&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;Sometimes you wish to run a test many times. For example, it might have some random factor in it. To cover many cases, you'd like to run it a lot of times. What options are there?&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;First, and the most obvious, is a loop. Run a for loop, and see it fail:&lt;/span&gt;&lt;br /&gt;
&lt;pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"&gt;&lt;a href="http://4.bp.blogspot.com/-pYYGEjL0Yc4/TZhN_AobLpI/AAAAAAAABs0/KlCHGD0to7Y/s1600/Example1.jpg" imageanchor="1" style="clear: right; float: right;"&gt;&lt;img border="0" height="116" src="http://4.bp.blogspot.com/-pYYGEjL0Yc4/TZhN_AobLpI/AAAAAAAABs0/KlCHGD0to7Y/s200/Example1.jpg" width="200" /&gt;&lt;/a&gt;&lt;code style="color: black; word-wrap: normal;"&gt; public class ExampleTest {  
      @Test  
      public void sometimesFail() {  
           for (int i = 0; i &amp;lt; 10; i++) {  
                int rand = new Random().nextInt(3);  
                if (rand % 3 == 0) {  
                     fail();  
                }  
           }  
      }  
 }  
&lt;/code&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;This will generate a single test in the tree. It will fail on the first error and won't give you an&amp;nbsp;assessment of&amp;nbsp;how many times it took till it failed.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;Another option is the Parametrized Runner. That would look like this:&lt;/span&gt;&lt;br /&gt;
&lt;pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"&gt;&lt;a href="http://1.bp.blogspot.com/-bWaJr9-d_0I/TZhPBrNDK-I/AAAAAAAABs4/dv_DmAVvLGQ/s1600/Example2.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"&gt;&lt;img border="0" height="200" src="http://1.bp.blogspot.com/-bWaJr9-d_0I/TZhPBrNDK-I/AAAAAAAABs4/dv_DmAVvLGQ/s200/Example2.jpg" width="173" /&gt;&lt;/a&gt;&lt;code style="color: black; word-wrap: normal;"&gt; @RunWith(Parameterized.class)  
 public class ExampleTest2 {  
      @Parameters  
      public static Collection&amp;lt;Object[]&amp;gt;
                                 generateParams() {  
           List&amp;lt;Object[]&amp;gt; params =
                   new ArrayList&amp;lt;Object[]&amp;gt;();  
           for (int i = 1; i &amp;lt;= 10; i++) {  
                params.add(new Object[] {i});  
           }  
           return params;  
      }  
        
      public ExampleTest2(int param) {}  
        
      @Test  
      public void sometimesFail() {  
           int rand = new Random().nextInt(3);  
           if (rand % 3 == 0) {  
                fail();  
           }  
      }  
 }  
&lt;/code&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;This is a nice solution, but the test is filled with&amp;nbsp;unnecessary code. Also, all the tests in the class will run for each parameter. What if I only want to repeat a single method?&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;Here is a nice solution, in my opinion:&lt;/span&gt;&lt;br /&gt;
&lt;pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"&gt;&lt;a href="http://1.bp.blogspot.com/-6Q9SeY9p740/TZhRg_-D0BI/AAAAAAAABs8/ynMnop66ZV4/s1600/Example3.jpg" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"&gt;&lt;img border="0" height="200" src="http://1.bp.blogspot.com/-6Q9SeY9p740/TZhRg_-D0BI/AAAAAAAABs8/ynMnop66ZV4/s200/Example3.jpg" width="171" /&gt;&lt;/a&gt;&lt;code style="color: black; word-wrap: normal;"&gt; @RunWith(ExtendedRunner.class)  
 public class ExampleTest3 {  
      @Test  
      @Repeat(10)  
      public void sometimesFail() {  
           int rand = new Random().nextInt(3);  
           if (rand % 3 == 0) {  
                fail();  
           }  
      }  
 }  
&lt;/code&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&amp;nbsp;Only state the method you want to repeat, and how many times to do this. The rest is done for you.&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;So, whats behind this code?&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;First, add an Annotation:&lt;/span&gt;&lt;br /&gt;
&lt;pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"&gt;&lt;code style="color: black; word-wrap: normal;"&gt; @Retention(RetentionPolicy.RUNTIME)  
 @Target({ElementType.METHOD})  
 public @interface Repeat {  
      int value();  
 }  
&lt;/code&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;Finally, lets add the Runner. This simply overrides the default JUnit runner (its a bit long, but not too much): &lt;/span&gt;&lt;br /&gt;
&lt;pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"&gt;&lt;code style="color: black; word-wrap: normal;"&gt; public class ExtendedRunner extends BlockJUnit4ClassRunner {  
   
      public ExtendedRunner(Class&amp;lt;?&amp;gt; klass) throws InitializationError {  
           super(klass);  
      }  
   
      @Override  
      protected Description describeChild(FrameworkMethod method) {  
           if (method.getAnnotation(Repeat.class) != null &amp;amp;&amp;amp;  
                     method.getAnnotation(Ignore.class) == null) {  
                return describeRepeatTest(method);  
           }  
           return super.describeChild(method);  
      }  
   
      private Description describeRepeatTest(FrameworkMethod method) {  
           int times = method.getAnnotation(Repeat.class).value();  
   
           Description description = Description.createSuiteDescription(  
                     testName(method) + " [" + times + " times]",  
                     method.getAnnotations());  
   
           for (int i = 1; i &amp;lt;= times; i++) {  
                description.addChild(Description.createTestDescription(  
                          getTestClass().getJavaClass(),  
                          "[" + i + "] " + testName(method)));  
           }  
           return description;  
      }  
   
      @Override  
      protected void runChild(final FrameworkMethod method, RunNotifier notifier) {  
           Description description = describeChild(method);  
             
           if (method.getAnnotation(Repeat.class) != null &amp;amp;&amp;amp;  
                     method.getAnnotation(Ignore.class) == null) {  
                runRepeatedly(methodBlock(method), description, notifier);  
           }  
           super.runChild(method, notifier);  
      }  
   
      private void runRepeatedly(Statement statement, Description description,  
                RunNotifier notifier) {  
           for (Description desc : description.getChildren()) {  
                runLeaf(statement, desc, notifier);  
           }  
      }  
        
 }  &lt;/code&gt;&lt;/pre&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;As always, here is the source code:&lt;span class="Apple-style-span" style="color: #3d85c6;"&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; color: #3d85c6; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px;"&gt;&lt;a href="http://tinyurl.com/3mu2zbc"&gt;http://tinyurl.com/3mu2zbc&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; font-family: Arial, Helvetica, sans-serif;"&gt;Also, you can&amp;nbsp;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;a href="http://www.amazon.com/s/?ie=UTF8&amp;amp;tag=code4kids-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;search-alias=aps&amp;amp;field-keywords=junit" target="_blank"&gt;Search Amazon.com for JUnit books&lt;/a&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;&lt;img alt="" border="0" height="1" src="http://www.assoc-amazon.com/e/ir?t=code4kids-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=" style="border-bottom-style: none !important; border-color: initial !important; border-left-style: none !important; border-right-style: none !important; border-top-style: none !important; border-width: initial !important; cursor: move; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important;" width="1" /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family: Arial, Helvetica, sans-serif;"&gt;.&lt;/span&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeHowTos/~4/4oVge8Rq-W8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://codehowtos.blogspot.com/feeds/5613906235032085506/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://codehowtos.blogspot.com/2011/04/run-junit-test-repeatedly.html#comment-form" title="13 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2138644806777257073/posts/default/5613906235032085506?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2138644806777257073/posts/default/5613906235032085506?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeHowTos/~3/4oVge8Rq-W8/run-junit-test-repeatedly.html" title="Run a JUnit test repeatedly" /><author><name>Igal Kreichman</name><uri>https://plus.google.com/108062574077972170259</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-jAR-7NAroHQ/AAAAAAAAAAI/AAAAAAAAFmc/_csjQQoLFfU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-pYYGEjL0Yc4/TZhN_AobLpI/AAAAAAAABs0/KlCHGD0to7Y/s72-c/Example1.jpg" height="72" width="72" /><thr:total>13</thr:total><feedburner:origLink>http://codehowtos.blogspot.com/2011/04/run-junit-test-repeatedly.html</feedburner:origLink></entry><entry gd:etag="W/&quot;AkEFQ3s4eCp7ImA9WhZQE00.&quot;"><id>tag:blogger.com,1999:blog-2138644806777257073.post-265238723492301030</id><published>2011-04-01T23:42:00.010+03:00</published><updated>2011-04-20T17:10:12.530+03:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2011-04-20T17:10:12.530+03:00</app:edited><category scheme="http://www.blogger.com/atom/ns#" term="games" /><category scheme="http://www.blogger.com/atom/ns#" term="datastructure" /><title>Building a Maze</title><content type="html">I always like to discover some cool ways of using a simple data structure or algorithm to simplify a piece of code.&amp;nbsp;Yesterday I overheard a conversation about Maze construction and the Disjoint-Set data structure, often considered used for optimization purposes only. So, I decided to try and implement this small example of how to quickly create a maze using this data structure.&lt;br /&gt;
&lt;br /&gt;
The Disjoint-Set, for those who are not familiar, is a data structure that allows to keep track of groups, letting you join groups and find the group to which a specific component belongs, all in O(Log*(n)).&lt;br /&gt;
For more info, read:&amp;nbsp;&lt;span class="Apple-style-span" style="color: #3d85c6;"&gt;&lt;a href="http://en.wikipedia.org/wiki/Disjoint-set_data_structure"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6;"&gt;http://en.wikipedia.org/wiki/Disjoint-set_data_structure&lt;/span&gt;&lt;/a&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Now, lets think of a maze as a grid of cells. For cells X and Y, you can reach from X to Y through the winding maze if there is a&amp;nbsp;continuous&amp;nbsp;route without walls between the two. So, lets start with a maze in which each cell has all its 4 walls built. The goal is to get a maze in which there is exactly 1 way to get from any cell X to any cell Y. Now lets add the data structure into the image. We start with a Disjoint-set that has a group for each of the cells. Each turn we destroy a wall between 2 cells that belong to different groups. This way we open exactly 1 passage between each of the cells in one group to each in the other. We know we've finished when there is exactly one group in the set.&lt;br /&gt;
&lt;br /&gt;
For those who like algorithms, this is&amp;nbsp;actually&amp;nbsp;an implementation of the Kruskal algorithm for finding Minimal Spanning Trees in graphs (if you take the grid cells as vertices and the walls as edges).&lt;br /&gt;
You can read more here:&amp;nbsp;&lt;a href="http://en.wikipedia.org/wiki/Kruskal%27s_algorithm"&gt;http://en.wikipedia.org/wiki/Kruskal%27s_algorithm&lt;/a&gt;&lt;br /&gt;
or you can look at some boos here:&amp;nbsp;&lt;a href="http://www.amazon.com/s/?ie=UTF8&amp;amp;tag=code4kids-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;search-alias=aps&amp;amp;field-keywords=algorithms" target="_blank"&gt;Books on Algorithms on Amazon&lt;/a&gt;&lt;img alt="" border="0" height="1" src="http://www.assoc-amazon.com/e/ir?t=code4kids-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=" style="border: none !important; margin: 0px !important; padding: 0px !important;" width="1" /&gt;&lt;br /&gt;
&lt;br /&gt;
Lets look at some code.&lt;br /&gt;
A simple implementation of the Disjoint-set:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"&gt;&lt;code style="color: black; word-wrap: normal;"&gt; public class DisjointSet {  
      private int[] set;  
      private int[] sizes;  
      private int size;  
      public DisjointSet(int size) {  
           this.set = new int[size];  
           for (int i = 0; i &amp;lt; size; i++) {  this.set[i] = i;  }  
           this.sizes = new int[size];  
           for (int i = 0; i &amp;lt; size; i++) {  this.sizes[i] = 1; }  
           this.size = size;  
      }  
      public int find(int item) {
           int root = item;
           // find the root
           while (set[root] != root) {
                 root = set[root];
           }
           // now shorten the paths
           int curr = item;
           while (set[curr] != root) {
                 set[curr] = root;
           }
           return root;
      }
      public int join(int item1, int item2) {  
           int group1 = find(item1);  
           int group2 = find(item2);  
           --size;  
           if (sizes[group1] &amp;gt; sizes[group2]) {  
                set[group2] = group1;  
                sizes[group1] += sizes[group2];  
                return group1;  
           } else {  
                set[group1] = group2;  
                sizes[group2] += sizes[group1];                 
                return group2;  
           }  
      }  
 }  
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
Now, lets build the maze:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style="background-image: URL(http://2.bp.blogspot.com/_z5ltvMQPaa8/SjJXr_U2YBI/AAAAAAAAAAM/46OqEP32CJ8/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"&gt;&lt;code style="color: black; word-wrap: normal;"&gt; Maze createRandomMaze(int rows, int columns) {  
           Maze maze = new Maze(rows, columns);  
           // create all walls  
           List&amp;lt;Wall&amp;gt; walls = maze.getAllInnerWalls();  
           // remove all the walls you can  
           DisjointSet diset = new DisjointSet(rows*columns);  
           while (diset.size() &amp;gt; 1) {  
                int wallIndex = random.nextInt(walls.size());  
                int cell1 = walls.get(wallIndex).cell1;  
                int cell2 = walls.get(wallIndex).cell2;  
                if (diset.find(cell1) != diset.find(cell2)) {  
                     // we can remove the wall  
                     maze.removeWall(walls.get(wallIndex));  
                     diset.join(cell1, cell2);  
                }  
                walls.remove(wallIndex);  
           }  
           return maze;  
      }  
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
And here is the result:&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-RzLVK_13O4g/TZY2Jn2f53I/AAAAAAAABsg/ScGU1loWkKQ/s1600/maze.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://2.bp.blogspot.com/-RzLVK_13O4g/TZY2Jn2f53I/AAAAAAAABsg/ScGU1loWkKQ/s320/maze.jpg" width="307" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For the full code and much much more, visit my open-source project at:&amp;nbsp;&lt;a href="https://code.google.com/p/ai4u/"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6;"&gt;https://code.google.com/p/ai4u/&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
The code in this post is located at:&lt;br /&gt;
Disjoint-Set:&amp;nbsp;&lt;a href="https://code.google.com/p/ai4u/source/browse/com.ai4u.util/trunk/src/com/ai4u/util/disjointSet/ArrayDisjointSet.java"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6;"&gt;https://code.google.com/p/ai4u/source/browse/com.ai4u.util/trunk/src/com/ai4u/util/disjointSet/ArrayDisjointSet.java&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
Maze:&amp;nbsp;&lt;a href="https://code.google.com/p/ai4u/source/browse/com.ai4u.util/trunk/src/com/ai4u/util/games/maze/Maze.java"&gt;&lt;span class="Apple-style-span" style="color: #3d85c6;"&gt;https://code.google.com/p/ai4u/source/browse/com.ai4u.util/trunk/src/com/ai4u/util/games/maze/Maze.java&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Well, this is it for my first post.&lt;br /&gt;
Please, send me any ideas, questions or comments to&amp;nbsp;&lt;a href="mailto:igal1987+blog@gmail.com"&gt;My Email&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/CodeHowTos/~4/5MZ7i3ubMk8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://codehowtos.blogspot.com/feeds/265238723492301030/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://codehowtos.blogspot.com/2011/04/building-maze.html#comment-form" title="4 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/2138644806777257073/posts/default/265238723492301030?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/2138644806777257073/posts/default/265238723492301030?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/CodeHowTos/~3/5MZ7i3ubMk8/building-maze.html" title="Building a Maze" /><author><name>Igal Kreichman</name><uri>https://plus.google.com/108062574077972170259</uri><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="//lh3.googleusercontent.com/-jAR-7NAroHQ/AAAAAAAAAAI/AAAAAAAAFmc/_csjQQoLFfU/s512-c/photo.jpg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-RzLVK_13O4g/TZY2Jn2f53I/AAAAAAAABsg/ScGU1loWkKQ/s72-c/maze.jpg" height="72" width="72" /><thr:total>4</thr:total><feedburner:origLink>http://codehowtos.blogspot.com/2011/04/building-maze.html</feedburner:origLink></entry></feed>
