<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' 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'><id>tag:blogger.com,1999:blog-3781349183261963248</id><updated>2024-11-08T12:54:22.281+05:30</updated><category term="Java"/><category term="Data Structure"/><category term="Collections"/><category term="Sorting"/><category term="JVM"/><category term="memory"/><category term="memory area allocation"/><category term="Comparable"/><category term="Comparator"/><category term="Exception and Error"/><category term="LinkedList"/><category term="Linux"/><category term="Matrix"/><category term="String"/><category term="final"/><category term="final keyword"/><category term="unix"/><category term="+"/><category term=".Z"/><category term=".tar.Z"/><category term="Bash"/><category term="Bubble Sort"/><category term="Command-line arguments"/><category term="Database"/><category term="Design patterns"/><category term="Error"/><category term="ExceptionInInitializerError"/><category term="IDE"/><category term="InsertionSort"/><category term="Intelij"/><category term="Iterable"/><category term="Iterator"/><category term="Java Keywords"/><category term="Multi-threading"/><category term="MyEclipse"/><category term="OOP"/><category term="Personal Note"/><category term="Quick Sort"/><category term="Recursion"/><category term="Shell"/><category term="Shell scripting"/><category term="StringBuffer"/><category term="StringBuilder"/><category term="Tree"/><category term="api"/><category term="ascii"/><category term="bytecode"/><category term="cohesion"/><category term="compress"/><category term="concat"/><category term="confllict"/><category term="coupling"/><category term="disassembling"/><category term="git"/><category term="github"/><category term="high cohesion"/><category term="immutable"/><category term="inline"/><category term="java.lang.ExceptionInInitializerError"/><category term="javac"/><category term="javap"/><category term="locking"/><category term="loose coupling"/><category term="main method"/><category term="merge"/><category term="mock"/><category term="naming convention"/><category term="native2ascii"/><category term="performance"/><category term="regex"/><category term="singleton"/><category term="sql"/><category term="synchronization"/><category term="try-catch-finally"/><category term="uncompress"/><category term="unicode"/><category term="variables"/><category term="wiremock"/><title type='text'>Little things worth stumbling upon!</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default?start-index=26&amp;max-results=25'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>42</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-4668286427519944450</id><published>2018-10-25T17:40:00.000+05:30</published><updated>2018-10-25T17:46:25.248+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="confllict"/><category scheme="http://www.blogger.com/atom/ns#" term="git"/><category scheme="http://www.blogger.com/atom/ns#" term="github"/><category scheme="http://www.blogger.com/atom/ns#" term="merge"/><title type='text'>Git: Resolve Conflicts in PR</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;graf graf--p&quot; name=&quot;baf5&quot;&gt;
What to do when GitHub does not allows you to merge a Pull Request due to conflicts!&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;i style=&quot;background-color: #fff2cc;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;First of all, rebase often to avoid this problem in most&amp;nbsp;cases.&lt;/span&gt;&lt;/i&gt;&lt;/blockquote&gt;
&lt;div class=&quot;graf graf--p&quot; name=&quot;12c1&quot;&gt;
Sharing bits from my experience. The solutions that worked for me are:&lt;/div&gt;
&lt;h3 class=&quot;graf graf--h3&quot; name=&quot;798a&quot; style=&quot;text-align: left;&quot;&gt;
&lt;/h3&gt;
&lt;h3 class=&quot;graf graf--h3&quot; name=&quot;798a&quot; style=&quot;text-align: left;&quot;&gt;
1. git&amp;nbsp;rebase&lt;/h3&gt;
&lt;div class=&quot;graf graf--p&quot; name=&quot;e44d&quot;&gt;
&lt;a class=&quot;markup--anchor markup--p-anchor&quot; data-href=&quot;https://git-scm.com/docs/git-rebase&quot; href=&quot;https://git-scm.com/docs/git-rebase&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;See documentation&lt;/a&gt;.&lt;/div&gt;
&lt;div class=&quot;graf graf--p&quot; name=&quot;f6af&quot;&gt;
The best way is to do it locally using terminal or command prompt. Lets say you raised a PR from origin/develop to upstream/develop branch and got conflict. Go to your command prompt, rebase master onto develop and push to develop branch. PR should get resolved.&lt;/div&gt;
&lt;div class=&quot;graf graf--p&quot; name=&quot;1b64&quot;&gt;
&lt;strong class=&quot;markup--strong markup--p-strong&quot;&gt;But this won’t work when the tips are just big time outdated.&lt;/strong&gt;&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt;
&lt;ul class=&quot;postList&quot;&gt;
&lt;li class=&quot;graf graf--li&quot; name=&quot;fd29&quot;&gt;I tried rebasing, and all I ended up was at same state but with alternated branches. Means the new branch was now compatible with master but not develop.&lt;/li&gt;
&lt;li class=&quot;graf graf--li&quot; name=&quot;50f5&quot;&gt;I also tried using GitHub’s web editor (link was present in PR comment near conflicting files), this also did not work, as after resolving, GitHub repeatedly asked me to &lt;em class=&quot;markup--em markup--li-em&quot;&gt;Start Over.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;graf graf--p&quot; name=&quot;4964&quot;&gt;
That’s when &lt;strong class=&quot;markup--strong markup--p-strong&quot;&gt;git merge&lt;/strong&gt; came to rescue.&lt;/div&gt;
&lt;h3 class=&quot;graf graf--h3&quot; name=&quot;89a1&quot; style=&quot;text-align: left;&quot;&gt;
&lt;strong class=&quot;markup--strong markup--h3-strong&quot;&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;h3 class=&quot;graf graf--h3&quot; name=&quot;89a1&quot; style=&quot;text-align: left;&quot;&gt;
&lt;strong class=&quot;markup--strong markup--h3-strong&quot;&gt;2. git&amp;nbsp;merge&lt;/strong&gt;&lt;/h3&gt;
&lt;div class=&quot;graf graf--p&quot; name=&quot;9a51&quot;&gt;
&lt;a class=&quot;markup--anchor markup--p-anchor&quot; data-href=&quot;https://git-scm.com/docs/git-merge&quot; href=&quot;https://git-scm.com/docs/git-merge&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;See documentation&lt;/a&gt;.&lt;/div&gt;
&lt;div class=&quot;graf graf--p&quot; name=&quot;dad9&quot;&gt;
Assuming, you are raising PR from your &lt;strong class=&quot;markup--strong markup--p-strong&quot;&gt;origin&lt;/strong&gt; to another remote called &lt;strong class=&quot;markup--strong markup--p-strong&quot;&gt;upstream&lt;/strong&gt;, perform the following in your local terminal or command prompt.&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span class=&quot;markup--em markup--pre-em&quot;&gt;git checkout develop&lt;/span&gt;&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span class=&quot;markup--em markup--pre-em&quot;&gt;git fetch upstream&lt;/span&gt;&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span class=&quot;markup--em markup--pre-em&quot;&gt;git merge upstream/develop&lt;/span&gt;&lt;/blockquote&gt;
&lt;div class=&quot;graf graf--p&quot; name=&quot;84bd&quot;&gt;
Resolve any conflicts and commit. The run below to check if branch is clean now.&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span class=&quot;markup--em markup--pre-em&quot;&gt;git status&lt;/span&gt;&lt;/blockquote&gt;
&lt;div class=&quot;graf graf--p&quot; name=&quot;4062&quot;&gt;
Once confirmed, execute.&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span class=&quot;markup--em markup--pre-em&quot;&gt;git push origin develop&lt;/span&gt;&lt;/blockquote&gt;
&lt;div class=&quot;graf graf--p&quot; name=&quot;cc0b&quot;&gt;
And then, raise PR from &lt;strong class=&quot;markup--strong markup--p-strong&quot;&gt;origin/develop&lt;/strong&gt; to &lt;strong class=&quot;markup--strong markup--p-strong&quot;&gt;upstream/develop&lt;/strong&gt;. You should be able to merge it now.&lt;/div&gt;
&lt;h3 class=&quot;graf graf--h3&quot; name=&quot;f7c6&quot;&gt;
&lt;/h3&gt;
&lt;h3 class=&quot;graf graf--h3&quot; name=&quot;f7c6&quot;&gt;
3. &lt;strong class=&quot;markup--strong markup--h3-strong&quot;&gt;Force&amp;nbsp;push&lt;/strong&gt;&lt;/h3&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
Use this option if and only if you are sole owner of &lt;strong class=&quot;markup--strong markup--blockquote-strong&quot;&gt;upstream&lt;/strong&gt; and are confident that you’ve got all commits in your &lt;strong class=&quot;markup--strong markup--blockquote-strong&quot;&gt;origin/develop&lt;/strong&gt;.&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;text-align: left;&quot;&gt;
&lt;span style=&quot;color: red;&quot;&gt;Force Push overwrites target branch with your pushed commits, and you won’t be able to reclaim whats lost in the process.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;graf graf--p&quot; name=&quot;b843&quot;&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;strong class=&quot;markup--strong markup--p-strong&quot;&gt;&lt;span class=&quot;markup--em markup--pre-em&quot; style=&quot;font-weight: 400;&quot;&gt;git push origin develop&lt;/span&gt;&lt;/strong&gt;&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;strong class=&quot;markup--strong markup--p-strong&quot;&gt;&lt;span class=&quot;markup--em markup--pre-em&quot; style=&quot;font-weight: 400;&quot;&gt;git checkout -b develop upstream/develop&lt;/span&gt;&lt;/strong&gt;&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;strong class=&quot;markup--strong markup--p-strong&quot;&gt;&lt;span class=&quot;markup--em markup--pre-em&quot; style=&quot;font-weight: 400;&quot;&gt;git reset --hard origin/develop&lt;/span&gt;&lt;/strong&gt;&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;strong class=&quot;markup--strong markup--p-strong&quot;&gt;&lt;span class=&quot;markup--em markup--pre-em&quot; style=&quot;font-weight: 400;&quot;&gt;git push upstream +develop&lt;/span&gt;&lt;/strong&gt;&lt;/blockquote&gt;
&lt;strong class=&quot;markup--strong markup--p-strong&quot;&gt;&lt;span class=&quot;markup--em markup--pre-em&quot; style=&quot;font-weight: 400;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;
&lt;strong class=&quot;markup--strong markup--p-strong&quot;&gt;Note:&lt;/strong&gt; &lt;strong class=&quot;markup--strong markup--p-strong&quot;&gt;origin&lt;/strong&gt; and &lt;strong class=&quot;markup--strong markup--p-strong&quot;&gt;upstream&lt;/strong&gt; are just regular remotes, former is your own remote repository and &lt;strong class=&quot;markup--strong markup--p-strong&quot;&gt;upstream&lt;/strong&gt; represents the base repository from which you have forked your’s. For example,&lt;/div&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span class=&quot;markup--em markup--pre-em&quot;&gt;upstream -&amp;gt; &lt;/span&gt;&lt;span class=&quot;markup--em markup--pre-em&quot;&gt;&lt;a class=&quot;markup--anchor markup--pre-anchor&quot; data-href=&quot;https://github.com/microsoft/msoffice&quot; href=&quot;https://github.com/microsoft/msoffice&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;https://github.com/microsoft/msoffice&lt;/a&gt;&lt;/span&gt;&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span class=&quot;markup--em markup--pre-em&quot;&gt;origin -&amp;gt; &lt;/span&gt;&lt;a class=&quot;markup--anchor markup--pre-anchor&quot; data-href=&quot;https://github.com/rajdeep/msoffice&quot; href=&quot;https://github.com/rajdeep/msoffice&quot; rel=&quot;noopener&quot; target=&quot;_blank&quot;&gt;&lt;span class=&quot;markup--em markup--pre-em&quot;&gt;https://github.com/rajdeep/msoffice&lt;/span&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;div class=&quot;graf graf--p&quot; name=&quot;b4f9&quot;&gt;
Thats it! I hope it helps. Let me know in comments if this worked or you have some other scenario or solution.&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/4668286427519944450/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2018/10/git-resolve-conflicts-in-pr.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/4668286427519944450'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/4668286427519944450'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2018/10/git-resolve-conflicts-in-pr.html' title='Git: Resolve Conflicts in PR'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-4137602579578440288</id><published>2017-08-16T02:24:00.000+05:30</published><updated>2017-08-16T02:28:39.467+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="api"/><category scheme="http://www.blogger.com/atom/ns#" term="mock"/><category scheme="http://www.blogger.com/atom/ns#" term="wiremock"/><title type='text'>Using Wiremock for quick and easy http mocks of your API</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; padding: 0px; text-size-adjust: auto;&quot;&gt;
I published this article at the turn of the year in my company&#39;s internal blog. I am republishing it here thinking it might be useful to outside world who are looking at a concise guide.&lt;/div&gt;
&lt;div style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; padding: 0px; text-size-adjust: auto;&quot;&gt;
WireMock is an HTTP mock server. At its core it is web server that can be primed to&lt;/div&gt;
&lt;ol style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin: 10px 0px 0px; text-size-adjust: auto;&quot;&gt;
&lt;li class=&quot;p1&quot;&gt;&lt;span class=&quot;s1&quot;&gt;serve canned responses to particular requests (stubbing) and&amp;nbsp;&lt;/span&gt;&lt;/li&gt;
&lt;li class=&quot;p1&quot;&gt;&lt;span class=&quot;s1&quot;&gt;that captures incoming requests so that they can be checked later (verification).&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
Imagine your application component is dependent on some other component for development or testing, here WireMock can come in and remove your dependency headaches.&lt;br /&gt;
You can agree the contract, design the stub and use it for your own component without tight integration with other component. This makes the development and testing much&lt;br /&gt;
easier and faster.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
WireMock also helps in situations when you want to test against a variety of parameters but you have programming limitations or other complexity/error factors. You can read more&lt;br /&gt;
in the official website.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
WireMock comes as a jar file that you can either&lt;/div&gt;
&lt;ul style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; list-style-type: square; margin: 10px 0px 0px; text-size-adjust: auto;&quot;&gt;
&lt;li&gt;use as a library in your Java code (or any JVM application), or&lt;/li&gt;
&lt;/ul&gt;
&lt;ul style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; list-style-type: square; margin: 10px 0px 0px; text-size-adjust: auto;&quot;&gt;
&lt;li&gt;run as a standalone process in your localhost or remote.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;All of WireMock’s features are accessible via its REST (JSON) interface and its Java API. Additionally, stubs can be configured via JSON files.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
Here we are talking about&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;basic info on running it as a standalone process.&lt;/strong&gt;&lt;/div&gt;
&lt;h1 class=&quot;p1&quot; id=&quot;UsingWiremockforquickandeasyhttpmocksofyourAPI-Runningtheserverwithstub&quot; style=&quot;border-bottom-color: rgb(204, 204, 204); color: #333333; font-family: Arial, sans-serif; font-size: 24px; font-weight: normal; line-height: 1.25; margin: 30px 0px 0px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;strong&gt;Running the server with stub&lt;/strong&gt;&lt;/h1&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;strong&gt;Step 1:&lt;/strong&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;Download the latest jar from&amp;nbsp;&lt;a class=&quot;external-link&quot; href=&quot;http://wiremock.org/&quot; rel=&quot;nofollow&quot; style=&quot;color: #3572b0; text-decoration-line: none;&quot;&gt;http://wiremock.org/&lt;/a&gt;.&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;strong&gt;Step 2&lt;/strong&gt;: Run the jar.&lt;/div&gt;
&lt;pre class=&quot;p1&quot; style=&quot;color: #333333; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;$ java -jar wiremock-standalone-2.4.1.jar&lt;/pre&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
By default this will start the mock server on http port 8080, which is available at:&lt;/div&gt;
&lt;pre class=&quot;p1&quot; style=&quot;color: #333333; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;http://&amp;lt;host&amp;gt;:8080&lt;/pre&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;When the WireMock server starts it creates two directories under the current one:&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;strong&gt;mappings&lt;/strong&gt;&lt;span class=&quot;s1&quot;&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;and&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;strong&gt;__files&lt;/strong&gt;&lt;span class=&quot;s2&quot;&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;in the same directory as of the jar by default.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;strong&gt;some_folder&lt;/strong&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |----&amp;nbsp;&lt;strong&gt;__files&lt;/strong&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |----&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;mappings&lt;/strong&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |----&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;wiremock-standalone-2.4.1.jar&lt;/strong&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;strong&gt;Step 3:&lt;/strong&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;Put the mapping and/or any stub response in a .json file inside&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;mappings&lt;/strong&gt;.&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;If you want to manage the stub in multiple files, drop it under&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;__files&lt;/strong&gt;, and map it against particular request in the .json file inside&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;mappings&lt;/strong&gt;.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;strong&gt;Step 4:&lt;/strong&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;For contents to be reflected, restart server.&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;strong&gt;Step 5:&lt;/strong&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;Use browser, or any client like Postman to test it. You can also integrate the end point in your application code like any normal URL.&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;blockquote style=&quot;border-left: 1px solid rgb(204, 204, 204); color: #707070; font-family: Arial, sans-serif; font-size: 14px; margin: 10px 0px 0px 19px; padding: 10px 20px; text-size-adjust: auto;&quot;&gt;
&lt;div class=&quot;p1&quot; style=&quot;padding: 0px;&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;Note: There are different flags like&amp;nbsp;&lt;code&gt;--port&lt;/code&gt;,&amp;nbsp;&lt;code&gt;--https-port&lt;/code&gt;&amp;nbsp;etc to start the application with that you can get from official website so as to match your requirements.&lt;/span&gt;&lt;/div&gt;
&lt;/blockquote&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;strong&gt;Example:&lt;/strong&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
For the following outcome:&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;span class=&quot;confluence-embedded-file-wrapper confluence-embedded-manual-size&quot; style=&quot;display: inline-block; max-width: none; position: relative;&quot;&gt;&lt;img alt=&quot;localhost snapshot&quot; class=&quot;confluence-embedded-image&quot; confluence-query-params=&quot;effects=border-simple,shadow-kn&quot; data-base-url=&quot;https://engineering.paypalcorp.com/confluence&quot; data-image-src=&quot;/confluence/download/attachments/295759551/image2017-1-3%200%3A52%3A4.png?version=1&amp;amp;modificationDate=1483384924000&amp;amp;api=v2&quot; data-linked-resource-container-id=&quot;295759551&quot; data-linked-resource-container-version=&quot;4&quot; data-linked-resource-content-type=&quot;image/png&quot; data-linked-resource-default-alias=&quot;image2017-1-3 0:52:4.png&quot; data-linked-resource-id=&quot;295759553&quot; data-linked-resource-type=&quot;attachment&quot; data-linked-resource-version=&quot;1&quot; data-unresolved-comment-count=&quot;0&quot; height=&quot;150&quot; src=&quot;https://engineering.paypalcorp.com/confluence/download/attachments/295759551/image2017-1-3%200%3A52%3A4.png?version=1&amp;amp;modificationDate=1483384924000&amp;amp;api=v2&amp;amp;effects=border-simple,shadow-kn&quot; style=&quot;border: 0px; display: block; margin: 0px 2px; max-width: none; padding: 0px;&quot; title=&quot;&quot; /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
We did the following:&lt;/div&gt;
&lt;ol style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin: 10px 0px 0px; text-size-adjust: auto;&quot;&gt;
&lt;li class=&quot;p1&quot;&gt;Created a&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;mapping.json&lt;/strong&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;file inside&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;mappings&lt;/strong&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;folder with following content.&lt;br /&gt;&lt;div class=&quot;p1&quot; style=&quot;margin-top: 10px; padding: 0px;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;strong&gt;&lt;code&gt;{&lt;/code&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&quot;request&quot;: {&lt;/code&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&quot;method&quot;: &quot;GET&quot;,&lt;/code&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&quot;url&quot;: &quot;/api/mytest&quot;&lt;/code&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; },&lt;/code&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&quot;response&quot;: {&lt;/code&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&quot;status&quot;: 200,&lt;/code&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&quot;body&quot;: &quot;More content\n&quot;&lt;/code&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; }&lt;/code&gt;}&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;margin-top: 10px; padding: 0px;&quot;&gt;
&lt;br /&gt;
&lt;span class=&quot;s1&quot;&gt;We have stubbed the response with &quot;More Content&quot; text here directly so we did not use&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;__files&lt;/strong&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;folder.&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;If any of the request method or URL does not matches, server will return 404.&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;margin-top: 10px; padding: 0px;&quot;&gt;
&lt;span class=&quot;s1&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;p1&quot;&gt;&lt;div class=&quot;p1&quot; style=&quot;padding: 0px;&quot;&gt;
Started the server:&lt;/div&gt;
&lt;pre class=&quot;p1&quot; style=&quot;margin-top: 10px; padding: 0px;&quot;&gt;$ java -jar wiremock-standalone-2.4.1.jar&lt;/pre&gt;
&lt;div style=&quot;margin-top: 10px; padding: 0px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
Here&#39;s the folder structure:&lt;/div&gt;
&lt;div style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;strong&gt;&lt;strong&gt;my_folder&lt;/strong&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |----&amp;nbsp;&lt;strong&gt;__files&lt;/strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |----&amp;nbsp;&lt;strong&gt;mappings&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |---- mapping.json&lt;/strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: black;&quot;&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |----&amp;nbsp;&lt;strong&gt;wiremock-standalone-2.4.1.jar&lt;/strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
We can do request matching based on request body or other request parameters also like URL, headers, method etc, and put in multiple mapping&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;.json&lt;/strong&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;files inside&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;mappings&lt;/strong&gt;.&lt;/div&gt;
&lt;div style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
When the stub is not simple and we want to keep them arranged in our server, we can put them in files inside&amp;nbsp;&lt;strong&gt;__files&lt;/strong&gt;, and provide their names inside the corresponding mapping files.&lt;/div&gt;
&lt;div style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
Following mapping gives an example of request body matching and maps it to a particular stub file.&lt;/div&gt;
&lt;div class=&quot;p1&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;strong&gt;&lt;code&gt;{&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&quot;request&quot;: {&lt;/code&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&quot;method&quot;:&amp;nbsp;&quot;POST&quot;,&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&quot;bodyPatterns&quot;:&amp;nbsp;[&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;contains&quot;:&quot;type:&amp;nbsp;CREDIT&quot;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ]&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; },&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&quot;response&quot;: {&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&quot;status&quot;:&amp;nbsp;200,&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &quot;bodyFileName&quot;:&quot;Credit_Response.json&quot;&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;code&gt;&amp;nbsp; &amp;nbsp; }&lt;/code&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;code&gt;}&lt;/code&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
The folder structure for above is:&lt;/div&gt;
&lt;div class=&quot;p2&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;strong&gt;&lt;strong&gt;my_folder&lt;/strong&gt;&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |----&amp;nbsp;&lt;strong&gt;__files&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |----&amp;nbsp;&lt;/strong&gt;&lt;/strong&gt;&lt;strong&gt;&lt;code&gt;Credit_Response.json&lt;/code&gt;&lt;/strong&gt;&lt;strong&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |----&amp;nbsp;&lt;strong&gt;mappings&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |---- mapping.json&lt;/strong&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; |----&lt;/strong&gt;&amp;nbsp;&lt;strong&gt;wiremock-standalone-2.4.1.jar&lt;/strong&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h1 class=&quot;p2&quot; id=&quot;UsingWiremockforquickandeasyhttpmocksofyourAPI-Verification&quot; style=&quot;border-bottom-color: rgb(204, 204, 204); color: #333333; font-family: Arial, sans-serif; font-size: 24px; font-weight: normal; line-height: 1.25; margin: 30px 0px 0px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;strong&gt;Verification&lt;/strong&gt;&lt;/h1&gt;
&lt;div class=&quot;p2&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
WireMock maintains the log of all requests in-memory since most recent server startup. This is in JSON format. This is a critical resource for auditing for success/failure cases.&lt;/div&gt;
&lt;ol style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin: 10px 0px 0px; text-size-adjust: auto;&quot;&gt;
&lt;li class=&quot;p2&quot;&gt;Hitting a GET request to&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;/__admin/requests&lt;/strong&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;will return all requests. For example: http://localhost:8080/__admin/requests.&lt;/li&gt;
&lt;li class=&quot;p2&quot;&gt;Hitting a GET request to&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;/__admin/requests/unmatched&lt;/strong&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;will return all unmatched requests.&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;p2&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
Similarly, there are other locations too. Refer official doc.&lt;/div&gt;
&lt;div class=&quot;p2&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div class=&quot;p2&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;strong&gt;Reference:&amp;nbsp;&lt;a class=&quot;external-link&quot; href=&quot;http://wiremock.org/&quot; rel=&quot;nofollow&quot; style=&quot;color: #3572b0; text-decoration-line: none;&quot;&gt;http://wiremock.org/&lt;/a&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;hr /&gt;
&lt;div class=&quot;p2&quot; style=&quot;color: #333333; font-family: Arial, sans-serif; font-size: 14px; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;Q. Doesn&#39;t seem to work for protobuff requests. What is the way around?&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;Info. Protobuff is a message transfer format like json or XML, from Google.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;background-color: white;&quot;&gt;A.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;For textual formats like&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong style=&quot;font-family: Arial, sans-serif;&quot;&gt;txt&lt;/strong&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;or&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong style=&quot;font-family: Arial, sans-serif;&quot;&gt;json&lt;/strong&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;, JSONPath can be used to identify some keywords in the request body. But this won&#39;t work for&lt;/span&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong style=&quot;font-family: Arial, sans-serif;&quot;&gt;protobuf&lt;/strong&gt;&lt;span class=&quot;Apple-converted-space&quot; style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;font-family: &amp;quot;arial&amp;quot; , sans-serif;&quot;&gt;or other formats where JSONPath can&#39;t traverse itself. So following is the way in my idea:&lt;/span&gt;&lt;br /&gt;
&lt;ol style=&quot;font-family: Arial, sans-serif; margin: 10px 0px 0px; text-size-adjust: auto;&quot;&gt;
&lt;li&gt;Generate stubs at own end aforehand, serialize them and place in&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;_files&lt;/strong&gt;&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;directory.&lt;/li&gt;
&lt;li&gt;For request matching, do either of the following:&lt;br /&gt;2.1. Include request header with key-value pairs that you&#39;d like to identify, and look for them in your mapping file. I&#39;d recommend for ease of use.&lt;br /&gt;2.2 For request body searching, you need to write custom matcher class that can deserialize, read through the request body and returns whether the identification was found or not, then based upon mapping, return the already placed stub.&lt;/li&gt;
&lt;li&gt;While returning response, return the mapped serialized file with appropriate headers such as&lt;span class=&quot;Apple-converted-space&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;strong&gt;Content-Type&lt;/strong&gt;.&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
&lt;div style=&quot;font-family: Arial, sans-serif; margin-top: 10px; padding: 0px; text-size-adjust: auto;&quot;&gt;
For complete control, like dynamic response generation etc, you need to write and place the whole implementation in wiremock and use (jar in java). This is an overkill for simple mocking purpose.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/4137602579578440288/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2017/08/using-wiremock-for-quick-and-easy-http.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/4137602579578440288'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/4137602579578440288'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2017/08/using-wiremock-for-quick-and-easy-http.html' title='Using Wiremock for quick and easy http mocks of your API'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-8310122913845700670</id><published>2017-02-20T12:29:00.000+05:30</published><updated>2017-02-20T12:31:27.310+05:30</updated><title type='text'>Stackoverflow Documentation | The way to go</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;span style=&quot;font-family: Trebuchet MS, sans-serif;&quot;&gt;Many months now, the Stackoverflow documentation is maturing with new examples and continuous edits to existing examples. It covers many programming languages, and should be on top of your TO-DOs if you are trying to broaden your skill set or even validate what you already have in your kitty.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Trebuchet MS, sans-serif;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Trebuchet MS, sans-serif;&quot;&gt;If you feel it&#39;s good, you should consider making it awesome for others. Ultimately, giving back to the community only makes sure of technology&#39;s global reach and rapid development. Click here:&amp;nbsp;&lt;a href=&quot;http://stackoverflow.com/documentation&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;http://stackoverflow.com/documentation&lt;/a&gt;.&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/8310122913845700670/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2017/02/stackoverflow-documentation.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/8310122913845700670'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/8310122913845700670'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2017/02/stackoverflow-documentation.html' title='Stackoverflow Documentation | The way to go'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-5042782924721784694</id><published>2016-09-01T21:49:00.002+05:30</published><updated>2016-09-01T21:58:52.769+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Recursion"/><title type='text'>Recursion is all about trust.</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;table cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;float: left; margin-right: 1em; text-align: left;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiou2Uvdb5_Qp1oc45DtBPOSaGNr4cZGSAsLhOxPKytl98maFiEiQmnMpQ-RrAz3xk-fGNO5hK8A5pyyHCPttnT1bqkk_Lp4uXlkU-RZQRcdivAYSi2MdwFadDQ51e-EMVW3nv8HdHiw6iw/s1600/recursive_dream.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; margin-bottom: 1em; margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;150&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiou2Uvdb5_Qp1oc45DtBPOSaGNr4cZGSAsLhOxPKytl98maFiEiQmnMpQ-RrAz3xk-fGNO5hK8A5pyyHCPttnT1bqkk_Lp4uXlkU-RZQRcdivAYSi2MdwFadDQ51e-EMVW3nv8HdHiw6iw/s200/recursive_dream.jpg&quot; width=&quot;200&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot; , sans-serif; font-size: xx-small; text-align: left;&quot;&gt;Image credit:&amp;nbsp;http://www.cr31.co.uk/logoarts/&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;span style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot; , sans-serif;&quot;&gt;
The secret of recursion is only one thing. Trust. Here&#39;s a neat example to show you what it exactly means. Quoting Stephan van Hulst from Code Ranch here.&lt;br /&gt;
&lt;br /&gt;
Say there&#39;s a long queue of people, and you want to know how many are in the queue. You can simply ask the guy in front of you how many people there are in the queue, and then add 1 for yourself. &lt;b&gt;You don&#39;t care how the guy in front of you got the answer, you just trust that it&#39;s correct.&lt;/b&gt; 

The guy in front uses the same technique. This goes on all the way until the guy at the front of the queue is asked how many people there are in the queue. The guy at the front sees no people in front of him, so he just reports 1. He is the base case.
&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;final class PersonInQueue {
 
  private final PersonInQueue next;
 
  int askForLengthOfQueue() {
    if (next == null)
      return 1;
 
    return next.askForLengthOfQueue() +1;
  }
}&lt;/pre&gt;
&lt;span style=&quot;font-family: &amp;quot;trebuchet ms&amp;quot; , sans-serif;&quot;&gt;
Here&#39;s the post: &lt;a href=&quot;https://coderanch.com/t/669698/java/java/Algorithm-explanation&quot;&gt;Algorithm explanation&lt;/a&gt; if you want to have a look. The question was on Tower of Hanoi.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/5042782924721784694/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2016/09/recursion-is-all-about-trust.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/5042782924721784694'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/5042782924721784694'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2016/09/recursion-is-all-about-trust.html' title='Recursion is all about trust.'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiou2Uvdb5_Qp1oc45DtBPOSaGNr4cZGSAsLhOxPKytl98maFiEiQmnMpQ-RrAz3xk-fGNO5hK8A5pyyHCPttnT1bqkk_Lp4uXlkU-RZQRcdivAYSi2MdwFadDQ51e-EMVW3nv8HdHiw6iw/s72-c/recursive_dream.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-8272066023147334197</id><published>2016-04-10T17:46:00.000+05:30</published><updated>2016-04-11T22:54:38.840+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="regex"/><title type='text'>IP v4 address matcher (regex)</title><content type='html'>&lt;div style=&quot;text-align: left&quot; dir=&quot;ltr&quot; trbidi=&quot;on&quot;&gt;IP address is a string in the form &quot;A.B.C.D&quot;, where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed. The length of A, B, C, or D can&#39;t be greater than 3. &lt;br&gt;&lt;br&gt;&lt;a href=&quot;http://regexr.com/3d6mb&quot; target=&quot;_blank&quot;&gt;&lt;img title=&quot;image&quot; style=&quot;border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px&quot; border=&quot;0&quot; alt=&quot;image&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGKSfku9hpRv45VEQinfFa-lR8cMHOh4_QhKYN9HRkZb4NSRleQvXtcoUuH9MyMlWpkqHaXddJMIGhFL8TFQilmSAAtB67KHQuOMq2m0l7enYoU75NBougV_fIvva-r0zEYKV3vsKnEaQ/?imgmax=800&quot; width=&quot;610&quot; height=&quot;282&quot;&gt;&lt;/a&gt;&lt;br&gt;The pattern is: &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot;&quot;&gt;^(?:(?:25[0-5]?|2[0-4]?[0-9]?|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]?|2[0-4]?[0-9]?|[01]?[0-9][0-9]?)$&lt;/span&gt;&lt;br&gt;&lt;br&gt;Java: &lt;br&gt;&lt;pre class=&quot;brush:java&quot;&gt;public class IPv4Regex {
	static final String ipV4Pattern = &quot;^(?:(?:25[0-5]?|2[0-4]?[0-9]?|[01]?[0-9][0-9]?)\\.){3}&quot;
			+ &quot;(?:25[0-5]?|2[0-4]?[0-9]?|[01]?[0-9][0-9]?)$&quot;;
	
	public static void main(String[] args) {
		System.out.println(&quot;000.123.234.245&quot;.matches(ipV4Pattern));
	}
}&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/8272066023147334197/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2016/04/ip-v4-address-matcher-regex.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/8272066023147334197'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/8272066023147334197'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2016/04/ip-v4-address-matcher-regex.html' title='IP v4 address matcher (regex)'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGKSfku9hpRv45VEQinfFa-lR8cMHOh4_QhKYN9HRkZb4NSRleQvXtcoUuH9MyMlWpkqHaXddJMIGhFL8TFQilmSAAtB67KHQuOMq2m0l7enYoU75NBougV_fIvva-r0zEYKV3vsKnEaQ/s72-c?imgmax=800" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-8354965188318733500</id><published>2016-03-08T23:41:00.001+05:30</published><updated>2016-03-08T23:41:26.396+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><category scheme="http://www.blogger.com/atom/ns#" term="Tree"/><title type='text'>Tree traversal notes</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
1. Inorder:&lt;br /&gt;
&lt;br /&gt;
- travel left subtree in inorder&lt;br /&gt;
- visit root&lt;br /&gt;
- travel right subtree in inorder&lt;br /&gt;
&lt;br /&gt;
Recursive approach:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;void inOrder() {
 if(root != null) {
  inOrder(root.left);
  System.out.println(root.data);
  inOrder(root.right);
 }
}
&lt;/pre&gt;
&lt;br /&gt;
Iterative approach using stacks:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;void inOrder() {

    Stack stack = new Stack();

    Node current = root;

    while(current!=null || !stack.isEmpty()) {
        //push to stack and move to left sub-tree
        if(current!=null){
            stack.push(current);
            current = current.left;
        }
        else { //we need to pop out nodes from stack and shift to its right sub-tree
            current = stack.pop(); //visited node
            System.out.println(current.data);
            current = current.right;
        }
    }
}
&lt;/pre&gt;
&lt;br /&gt;
2. Pre-order:&lt;br /&gt;
&lt;br /&gt;
- visit root&lt;br /&gt;
- travel left subtree in preorder&lt;br /&gt;
- travel right subtree in preorder&lt;br /&gt;
&lt;br /&gt;
Recursive approach:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;void preOrder() {
    if(root != null) {
        System.out.println(root.data);
        preOrder(root.left);
        preOrder(root.right);
    }
}
&lt;/pre&gt;
&lt;br /&gt;
Iterative approach using stacks:&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;void inOrder() {

    Stack stack = new Stack();

    Node current = root;

    while(current!=null || !stack.isEmpty()) {
        //print each value, then push its right subtree to stack and move to left subtree
        if(current!=null){
            System.out.println(current.data); //visited node
            stack.push(current.right); //push right subtree
            current = current.left; //shift to left subtree
        }
        else {
            current = stack.pop();
        }
    }
}
&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/8354965188318733500/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2016/03/tree-traversal-notes.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/8354965188318733500'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/8354965188318733500'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2016/03/tree-traversal-notes.html' title='Tree traversal notes'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-6420560442864505473</id><published>2016-02-12T20:47:00.000+05:30</published><updated>2016-02-13T00:15:25.368+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Collections"/><category scheme="http://www.blogger.com/atom/ns#" term="Iterable"/><category scheme="http://www.blogger.com/atom/ns#" term="Iterator"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="LinkedList"/><title type='text'>Designing your own iterable stuff</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;tr_bq&quot; dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Or, simply, implementing iterator logic in your own class, on which you can iterate or even use for-each loop. Because, for-each works only on Iterator based collections.&lt;br /&gt;
Following is a generic &lt;b&gt;Java 7&lt;/b&gt; code that takes into account a simple custom linked list implementation that is generic and can be iterated over, using following steps.&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
1. The stuff we want to iterate upon has to be &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;Iterable&lt;/span&gt;&amp;nbsp;and expose&lt;br /&gt;
&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;iterator()&lt;/span&gt;.&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
2. Design a&amp;nbsp;&lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;java.util.Iterator&amp;nbsp;&lt;/span&gt;by overriding &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;hasNext()&lt;/span&gt;, &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;next()&lt;/span&gt; and &lt;span style=&quot;font-family: &amp;quot;courier new&amp;quot; , &amp;quot;courier&amp;quot; , monospace;&quot;&gt;remove()&lt;/span&gt;.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;/blockquote&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package org.algorithms.linkedlist;

import java.util.Iterator;
import java.util.NoSuchElementException;


public class LinkedList&amp;lt;T&amp;gt; implements Iterable&amp;lt;T&amp;gt; {

    Node&amp;lt;T&amp;gt; head, current;

    private static class Node&amp;lt;T&amp;gt; {
        T data;
        Node&amp;lt;T&amp;gt; next;

        Node(T data) {
            this.data = data;
        }
    }

    public LinkedList(T data) {
        head = new Node&amp;lt;&amp;gt;(data);
    }

    public Iterator&amp;lt;T&amp;gt; iterator() {
        return new LinkedListIterator();
    }

    private class LinkedListIterator implements Iterator&amp;lt;T&amp;gt; {

        Node&amp;lt;T&amp;gt; node = head;

        @Override
        public boolean hasNext() {
            return node != null;
        }

        @Override
        public T next() {
            if (!hasNext())
                throw new NoSuchElementException();
            Node&amp;lt;T&amp;gt; prevNode = node;
            node = node.next;
            return prevNode.data;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException(&quot;Removal logic not implemented.&quot;);
        }
    }

    public void add(T data) {
        Node current = head;
        while (current.next != null)
            current = current.next;
        current.next = new Node&amp;lt;&amp;gt;(data);
    }

}

class App {
    public static void main(String[] args) {

        LinkedList&amp;lt;Integer&amp;gt; list = new LinkedList&amp;lt;&amp;gt;(1);
        list.add(2);
        list.add(4);
        list.add(3);

        //Test #1
        System.out.println(&quot;using Iterator:&quot;);
        Iterator&amp;lt;Integer&amp;gt; itr = list.iterator();
        while (itr.hasNext()) {
            Integer i = itr.next();
            System.out.print(i + &quot; &quot;);
        }

        //Test #2
        System.out.println(&quot;\n\nusing for-each:&quot;);
        for (Integer data : list) {
            System.out.print(data + &quot; &quot;);
        }
    }
}&lt;/pre&gt;
&lt;/div&gt;
&lt;br /&gt;
Output&lt;br /&gt;
&lt;blockquote&gt;
using Iterator:&lt;br /&gt;
1 2 4 3&lt;br /&gt;
using for-each:&lt;br /&gt;
1 2 4 3&amp;nbsp;&lt;/blockquote&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/6420560442864505473/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2016/02/designing-your-own-iterable-stuff.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/6420560442864505473'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/6420560442864505473'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2016/02/designing-your-own-iterable-stuff.html' title='Designing your own iterable stuff'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-5767004500928049828</id><published>2015-06-28T23:54:00.001+05:30</published><updated>2015-07-01T02:31:10.603+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Collections"/><category scheme="http://www.blogger.com/atom/ns#" term="Comparable"/><category scheme="http://www.blogger.com/atom/ns#" term="Comparator"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Sorting"/><title type='text'>Sorting Key-Value pairs in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Saw making your own sorting logic by implementing Comparable or Comparator? If not, &lt;a href=&quot;http://javabambino.blogspot.in/2015/06/comparable-and-comparator-when-and-how.html&quot;&gt;click here&lt;/a&gt;. Now, how about sorting key-value pairs! Well, we need just a little code modification. See following code sample that sorts key-value pairs, first by key, then values. Look at the &lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;compareTo&lt;/span&gt; method implementation.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package collections;

import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Sort by key first and then by value
 */
public class KeyValueSortingApp {
    public static void main(String[] args) {
        SortedSet&amp;lt;KeyValuePair&amp;gt; sortedSet = new TreeSet&amp;lt;KeyValuePair&amp;gt;();
        sortedSet.add(new KeyValuePair(&quot;icici&quot;,400));
        sortedSet.add(new KeyValuePair(&quot;icici&quot;,200));
        sortedSet.add(new KeyValuePair(&quot;icici&quot;,50));
        sortedSet.add(new KeyValuePair(&quot;sbi&quot;,320));
        sortedSet.add(new KeyValuePair(&quot;icici&quot;,300));
        sortedSet.add(new KeyValuePair(&quot;sbi&quot;,100));

        for (KeyValuePair pair : sortedSet) {
            System.out.println(pair.key + &quot;:&quot; + pair.value);
        }
    }
}

class KeyValuePair implements Comparable&amp;lt;KeyValuePair&amp;gt; {
    String key;
    int value;
    public KeyValuePair(String key, int value) {
        super();
        this.key = key;
        this.value = value;
    }
    @Override
    public int compareTo(KeyValuePair o) {
        //sort by key, then value
        return key.equalsIgnoreCase(o.key) ? 
            value-o.value : 
            key.compareToIgnoreCase(o.key);
    }
}&lt;/pre&gt;
Output:&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;icici:50&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;icici:200&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;icici:300&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;icici:400&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;sbi:100&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;sbi:320&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/5767004500928049828/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2015/06/sorting-key-value-pairs-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/5767004500928049828'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/5767004500928049828'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2015/06/sorting-key-value-pairs-in-java.html' title='Sorting Key-Value pairs in Java'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-5206653061496018601</id><published>2015-06-07T04:11:00.000+05:30</published><updated>2015-06-27T22:47:07.793+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Collections"/><category scheme="http://www.blogger.com/atom/ns#" term="Comparable"/><category scheme="http://www.blogger.com/atom/ns#" term="Comparator"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Comparable and Comparator- When and how to use? [Concise]</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
When there comes the need of sorting objects, be it using Collections.sort() or just adding elements to Treeset etc, java.lang.Comparable and java.util.Comparator come into picture. For default sorting of objects in the collection, we need to implement Comparable and override below method in that class, unless we are using some pre-defined class that already implements Comparable like String, Integer etc.,
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;int compareTo(T o)
&lt;/pre&gt;
If we want to provide an external sorting logic to override the default one, the class needs to implement Comparator interface and override the following method:
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;int compare(T o1, T o2)
&lt;/pre&gt;
&lt;b&gt;The question: Why use Comparator when we already have Comparable?&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Answer:&lt;/b&gt; If you have authored that class, then you can give a default sorting behavior by implementing Comparable and use that logic to sort whenever needed. Now, let&#39;s say that you are using an existing class whose source you can not modify and the default sorting logic of which you are not satisfied with. What now? Just write a Comparator using your own sorting process and use that instance to sort. See below sample programs.&lt;br /&gt;
&lt;br /&gt;
The API information is here:&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot; style=&quot;text-align: left;&quot;&gt;
1. &lt;a href=&quot;https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html&quot;&gt;java.lang.Comparable&lt;/a&gt;&lt;br /&gt;
2. &lt;a href=&quot;https://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html&quot;&gt;Java.util.Comparator&lt;/a&gt;&lt;/blockquote&gt;
Lets dig into this with our custom class.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre class=&quot;brush:java&quot;&gt;package com.blogspot.javabambino.collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Student implements Comparable&amp;lt;Student&amp;gt; {
    String name;
    int rollNumber;
    int rank;

    public Student(String name, int rollNumber, int rank) {
        this.name = name;
        this.rollNumber = rollNumber;
        this.rank = rank;
    }

    //default sorting based on rollNumber
    public int compareTo(Student student) {
        return this.rollNumber - student.rollNumber;
    }

    public String toString(){
        return name;
    }
}

public class ComparableAndComparatorTest {
    public static void main(String[] args) {
        Student s1 = new Student(&quot;Abhi&quot;,1,3);
        Student s2 = new Student(&quot;Asha&quot;,2,10);
        Student s3 = new Student(&quot;Jacky&quot;,3,7);
        Student s4 = new Student(&quot;Ron&quot;,4,10);

        List&amp;lt;Student&amp;gt; students = new ArrayList&amp;lt;Student&amp;gt;();
        students.add(s4);
        students.add(s2);
        students.add(s3);
        students.add(s1);

        System.out.println(&quot;Before sorting:&quot;);
        System.out.println(students + &quot;\n&quot;);

        //1. This uses default comparison, from Students class
        Collections.sort(students);

        System.out.println(&quot;Using default sorting (rollnumber):&quot;);
        System.out.println(students + &quot;\n&quot;);

        //2. Providing a comparator to override default comparison
        Comparator&amp;lt;Student&amp;gt; rankComparator = new Comparator&amp;lt;Student&amp;gt;() {
            @Override
            public int compare(Student stud1, Student stud2) {
                int diff = stud1.rank - stud2.rank;
                if (diff==0)
                    //if rank is same, sort by rollnumber
                    diff = stud1.rollNumber-stud2.rollNumber;
                return diff;
            }
        };

        Collections.sort(students,rankComparator);

        System.out.println(&quot;Using overridden sorting logic (rank, then rollnumber): &quot;);
        System.out.println(students);
    }
}
&lt;/pre&gt;
Our input was in following sequence (just for readability):&lt;br /&gt;
&lt;table&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Rollnumber&lt;/th&gt;&lt;th&gt;Rank&lt;/th&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Ron&lt;/td&gt;&lt;td&gt;4&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Asha&lt;/td&gt;&lt;td&gt;2&lt;/td&gt;&lt;td&gt;10&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Jacky&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;td&gt;7&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Abhi&lt;/td&gt;&lt;td&gt;1&lt;/td&gt;&lt;td&gt;3&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
and the output is:
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Before sorting:&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;[Ron, Asha, Jacky, Abhi]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Using default sorting (rollnumber):&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;[Abhi, Asha, Jacky, Ron]&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Using overridden sorting logic (rank, then rollnumber):&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;[Abhi, Jacky, Asha, Ron]&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/5206653061496018601/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2015/06/comparable-and-comparator-when-and-how.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/5206653061496018601'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/5206653061496018601'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2015/06/comparable-and-comparator-when-and-how.html' title='Comparable and Comparator- When and how to use? [Concise]'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-5879291947730635071</id><published>2015-01-29T17:02:00.001+05:30</published><updated>2015-01-29T19:18:51.360+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="ascii"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="native2ascii"/><category scheme="http://www.blogger.com/atom/ns#" term="unicode"/><title type='text'>native2ascii - Native-to-ASCII Converter in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;This utility converts a file with characters in any supported character
encoding to one with ASCII and/or Unicode escapes, or visa versa.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
You already have this tool if you have JDK. It is present in your JDK installation&amp;nbsp;&lt;b&gt;&lt;i&gt;bin&lt;/i&gt;&lt;/b&gt;&amp;nbsp;folder. Its recommended to add JAVA_HOME to your PATH environment variable for access of java tools throughout your machine.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: #444444; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt;&quot;&gt;native2ascii&lt;/span&gt;&lt;span style=&quot;color: #444444; font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt;&quot;&gt; [options] [inputfile [outputfile]]&lt;/span&gt;&lt;/b&gt;&lt;span style=&quot;color: #444444; font-family: &amp;quot;Courier New&amp;quot;; font-size: 9.0pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;This is useful in translation of some string (error messages
or labels or help text&amp;nbsp;&lt;/span&gt;etc.&lt;span style=&quot;font-family: inherit;&quot;&gt;) to Unicode format&amp;nbsp;which is recognized by the Java
compiler. Java tools cannot process characters other than Latin-1 or Unicode,
so this &lt;/span&gt;&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;&lt;u&gt;&lt;span style=&quot;font-size: 10pt;&quot;&gt;&lt;b&gt;native2ascii&lt;/b&gt;&lt;/span&gt;&lt;/u&gt;
&lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;tool comes handy to convert your strings to Unicode format. I encountered this
in regards to translating a few error messages. The obvious benefit is that most (or any) of the text editors or IDEs would support simple unicoded characters rather than different locales, without the need to install locale packages. And &lt;a href=&quot;http://www.javatpoint.com/unicode-system-in-java&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;this guy&lt;/a&gt; also tells us something he knows about it!&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;An example usage if both files are in same location is as below:&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt;&quot;&gt;&amp;gt;&lt;b&gt;native2ascii
-encoding utf8 otherlocale.txt translated.txt&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpBCWxSFY7ZJiC1JL938aBhJisZWHfVlAK3bMW0XDEaruVpgMZboZnhJUa2K2DWzSa6jMYtXwC6fHlzWggu-XgofcKkCNdxHbbK1O8v8bEkGR9ZS60DtrJiqsicsb36crwJRKmqxYrK1qI/s1600/native2ascii.png&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpBCWxSFY7ZJiC1JL938aBhJisZWHfVlAK3bMW0XDEaruVpgMZboZnhJUa2K2DWzSa6jMYtXwC6fHlzWggu-XgofcKkCNdxHbbK1O8v8bEkGR9ZS60DtrJiqsicsb36crwJRKmqxYrK1qI/s1600/native2ascii.png&quot; height=&quot;235&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;b&gt;Read More:&lt;/b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraph&quot; style=&quot;mso-list: l0 level1 lfo1; text-indent: -.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;1.&lt;span style=&quot;font-size: 7pt; font-stretch: normal;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;!--[endif]--&gt;&lt;a href=&quot;https://docs.oracle.com/javase/7/docs/technotes/tools/solaris/native2ascii.html&quot;&gt;https://docs.oracle.com/javase/7/docs/technotes/tools/solaris/native2ascii.html&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoListParagraph&quot; style=&quot;mso-list: l0 level1 lfo1; text-indent: -.25in;&quot;&gt;
&lt;!--[if !supportLists]--&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;2.&lt;span style=&quot;font-size: 7pt; font-stretch: normal;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;!--[endif]--&gt;&lt;a href=&quot;http://www.cs.mun.ca/~michael/java/jdk1.1-beta2-docs/tooldocs/win32/native2ascii.html&quot;&gt;http://www.cs.mun.ca/~michael/java/jdk1.1-beta2-docs/tooldocs/win32/native2ascii.html&lt;/a&gt;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/5879291947730635071/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2015/01/native2ascii-native-to-ascii-converter.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/5879291947730635071'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/5879291947730635071'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2015/01/native2ascii-native-to-ascii-converter.html' title='native2ascii - Native-to-ASCII Converter in Java'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpBCWxSFY7ZJiC1JL938aBhJisZWHfVlAK3bMW0XDEaruVpgMZboZnhJUa2K2DWzSa6jMYtXwC6fHlzWggu-XgofcKkCNdxHbbK1O8v8bEkGR9ZS60DtrJiqsicsb36crwJRKmqxYrK1qI/s72-c/native2ascii.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-6942728343651996928</id><published>2014-12-20T21:00:00.000+05:30</published><updated>2014-12-20T21:19:41.714+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="IDE"/><category scheme="http://www.blogger.com/atom/ns#" term="Intelij"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Falling in love with InteliJ Idea Community IDE</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
The local file history that keeps a track on my changes to a file, I am all smiles now! :~)&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizGp66OdVXKbujD-fqvMRE-1In4yABtcjPfz8CPZfWh3W0ZdptSIUoelDJoEAjkLgUd1WcA39LjpDpZeP5_k7k3Ytu85_8pNvv6881xLcB65o2apidLW7iuK21ErmSPhgRNK_NvoVCdn1Z/s1600/Snap+2014-12-20+at+21.16.15.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizGp66OdVXKbujD-fqvMRE-1In4yABtcjPfz8CPZfWh3W0ZdptSIUoelDJoEAjkLgUd1WcA39LjpDpZeP5_k7k3Ytu85_8pNvv6881xLcB65o2apidLW7iuK21ErmSPhgRNK_NvoVCdn1Z/s640/Snap+2014-12-20+at+21.16.15.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Local History - Click on the image to enlarge&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/6942728343651996928/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2014/12/falling-in-love-with-intelij-idea.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/6942728343651996928'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/6942728343651996928'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2014/12/falling-in-love-with-intelij-idea.html' title='Falling in love with InteliJ Idea Community IDE'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizGp66OdVXKbujD-fqvMRE-1In4yABtcjPfz8CPZfWh3W0ZdptSIUoelDJoEAjkLgUd1WcA39LjpDpZeP5_k7k3Ytu85_8pNvv6881xLcB65o2apidLW7iuK21ErmSPhgRNK_NvoVCdn1Z/s72-c/Snap+2014-12-20+at+21.16.15.png" height="72" width="72"/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-1515474342873168800</id><published>2014-05-20T17:02:00.000+05:30</published><updated>2015-01-29T19:17:35.303+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Collections"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Java Map/Collections Cheat Sheet : Simply &#39;When and What to use&#39;?</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: left;&quot;&gt;
This is a notoriously simple when and what to use Java collection API cheat sheet, containing most common implementations.&lt;/div&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI2EsLsYzo_WQBPe7dppLxhRZTcsl2S1uYdMlzpV98zgRN13zbNjYn7bQIdL1BBOTL8mFbQExicaOFMGb66JkrKeYgSVlqGwe_-xx_ymjNxlD9e3pkSFYFAjap2oYRq4KkPkbV0yZ5MiOG/s1600/java-map-collection-cheat-sheet.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Java Map/Collection Cheat Sheet image&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI2EsLsYzo_WQBPe7dppLxhRZTcsl2S1uYdMlzpV98zgRN13zbNjYn7bQIdL1BBOTL8mFbQExicaOFMGb66JkrKeYgSVlqGwe_-xx_ymjNxlD9e3pkSFYFAjap2oYRq4KkPkbV0yZ5MiOG/s1600/java-map-collection-cheat-sheet.gif&quot; height=&quot;485&quot; title=&quot;&quot; width=&quot;625&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;This is from a blog that I stumbled upon today! &lt;/span&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;I liked it and it deserves a share unless the author says - Come on bambino, time for some royalty!&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Thank you Sergiy Kovalchuk for your&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;http://www.sergiy.ca/guide-to-selecting-appropriate-map-collection-in-java/&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;blog article&lt;/span&gt;&lt;/a&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp;!&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/1515474342873168800/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2014/05/java-mapcollections-cheat-sheet-simply.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/1515474342873168800'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/1515474342873168800'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2014/05/java-mapcollections-cheat-sheet-simply.html' title='Java Map/Collections Cheat Sheet : Simply &#39;When and What to use&#39;?'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgI2EsLsYzo_WQBPe7dppLxhRZTcsl2S1uYdMlzpV98zgRN13zbNjYn7bQIdL1BBOTL8mFbQExicaOFMGb66JkrKeYgSVlqGwe_-xx_ymjNxlD9e3pkSFYFAjap2oYRq4KkPkbV0yZ5MiOG/s72-c/java-map-collection-cheat-sheet.gif" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-1573571560983277902</id><published>2014-05-19T00:26:00.001+05:30</published><updated>2015-03-31T14:08:23.044+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="String"/><title type='text'>Find the occurrence of words in a given string</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;IDEONE:&lt;/b&gt; &lt;a href=&quot;http://ideone.com/9SENHc&quot; target=&quot;_blank&quot;&gt;http://ideone.com/9SENHc&lt;/a&gt; &lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package com.learning.collection;
import java.util.*;
/**
 * Count frequency of words occurring in a string
 */
public class MapExample {
    public static void main(String[] args){
        String str = &quot;ika pika ting tong ting tong me&quot;;
        String[] strr = str.split(&quot;\\s&quot;);
        workMap(strr);
    }

    static void workMap(String...words){
        Map map = new HashMap();
        Integer ONE = new Integer(1);

        for(String word : words){
            Integer frequency = (Integer)map.get(word);
            /*
            &#39;frequency&#39; is the count of the words.
            For a new word getting added to the Map, we set its frequency as 1.
            For existing words, we take their existing frequency (value in the map)
            and increment it and put back into the map correspondint to that word (key in the map)
             */
            frequency = (frequency == null) ? ONE : new Integer(frequency.intValue() + 1);
            map.put(word, frequency);
        }
        System.out.println(&quot;Unsorted:&quot;);
        System.out.println(map);

        Map sortedMap = new TreeMap(map);
        System.out.println(&quot;Sorted:&quot;);
        System.out.println(sortedMap);
    }
}&lt;/pre&gt;
&lt;b&gt;Output:&lt;/b&gt;&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Unsorted:
{ika=1, pika=1, ting=2, tong=2, me=1}&lt;br /&gt;Sorted:
{ika=1, me=1, pika=1, ting=2, tong=2}&lt;/span&gt;&lt;/blockquote&gt;
&lt;br /&gt;
&lt;br /&gt;
--Suggestion by Dr. Heinz Kabutz (Java Champion)&lt;br /&gt;
&lt;b&gt;How you would count words in Java 8&lt;/b&gt; :-)

And if you want to do it in parallel, just add parallel() into the stream...
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;import java.util.*;
import java.util.function.*;
import java.util.stream.*;

public class MapExample {
  public static void main(String[] args) {
    String str = &quot;hello world how are you how is life i am into ascent&quot;;

    Map&amp;lt;String, Integer&amp;gt; map = Arrays.stream(str.split(&quot;\\s&quot;))
        .collect(Collectors.toMap(
            Function.identity(),
            n -&amp;gt; 1,
            (n1, n2) -&amp;gt; n1 + n2,
            TreeMap::new));
    System.out.println(&quot;map = &quot; + map);
  }
}&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/1573571560983277902/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2014/05/find-occurrence-of-words-in-given-string.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/1573571560983277902'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/1573571560983277902'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2014/05/find-occurrence-of-words-in-given-string.html' title='Find the occurrence of words in a given string'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-3566596499616425829</id><published>2014-04-30T17:43:00.001+05:30</published><updated>2015-01-29T19:32:05.401+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bash"/><category scheme="http://www.blogger.com/atom/ns#" term="Linux"/><category scheme="http://www.blogger.com/atom/ns#" term="Shell"/><category scheme="http://www.blogger.com/atom/ns#" term="Shell scripting"/><category scheme="http://www.blogger.com/atom/ns#" term="unix"/><title type='text'>Finding the JAR files which contain a specific .class file on Linux machine</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;This program searches through all .jar files in the current directory, and in any sub-directories, looking for the class that you specify.&lt;/b&gt; This can be very handy if you need to use a class in a Java program, but aren&#39;t sure which .jar file contains it.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVdl7TcSsWOi1Ut99nSdyShhQYGcT7Om9jEH1g66vlYISltHY4VioGWEbupAiVuGXFB5OLuQ8Jw8FOly8R6Z-aO-rRifHppyZdUxKKjapW76fCfaG-pbIAYY23q5GOJUOEWKhpHL5K-i0G/s1600/java_linux.jpg&quot; imageanchor=&quot;1&quot; style=&quot;clear: left; float: left; margin-bottom: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVdl7TcSsWOi1Ut99nSdyShhQYGcT7Om9jEH1g66vlYISltHY4VioGWEbupAiVuGXFB5OLuQ8Jw8FOly8R6Z-aO-rRifHppyZdUxKKjapW76fCfaG-pbIAYY23q5GOJUOEWKhpHL5K-i0G/s1600/java_linux.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Logic of script:&lt;/b&gt; The key is to get a list of all the jar files in present directory, open each jar archive and search it with our input classname, and for all positive output, print the name of the jar file on screen.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;
Sample Usage:
&lt;pre class=&quot;brush:bash&quot;&gt;jar -tf rt.jar | grep &quot;Object.class&quot;&lt;/pre&gt;
&lt;b&gt;&lt;br /&gt;&lt;/b&gt;
&lt;b&gt;Note:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
1. After downloading, rename the file to &quot;JarSearch.sh&quot;. Transfer the &lt;b&gt;JarSearch.sh&lt;/b&gt; file to Linux machine in &lt;b&gt;ASCII &lt;/b&gt;mode to the location where you want to search. Give permission:
&lt;br /&gt;
&lt;pre class=&quot;brush:bash&quot;&gt;chmod -x JarSearch.sh&lt;/pre&gt;
Sometimes, before giving permission, it may be required to convert the file format using below command to UNIX format. This will not affect the functionality.
&lt;br /&gt;
&lt;pre class=&quot;brush:bash&quot;&gt;dos2unix JarSearch.sh&lt;/pre&gt;
&lt;br /&gt;
2. Before using the script, verify that the JAVA_HOME environment variable is set correctly on your system.
&lt;br /&gt;
&lt;pre class=&quot;brush:bash&quot;&gt;echo $JAVA_HOME&lt;/pre&gt;
If not set, set it to JDK installation directory.&lt;br /&gt;
&lt;br /&gt;
3. &lt;b&gt;Information&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;About:&lt;/b&gt; Searches .jar in current path for a specified class file including sub-directories.&lt;br /&gt;
&lt;b&gt;Usage:&lt;/b&gt; ./JarSearch.sh class_name&lt;br /&gt;
&lt;b&gt;Example:&lt;/b&gt; ./JarSearch.sh Object&lt;br /&gt;
&lt;b&gt;Caution:&lt;/b&gt; Give exact class name without .class extension or package inormation. This searches for file name.&lt;br /&gt;
&lt;b&gt;Known issue:&lt;/b&gt; Can not be used to search inner classes due to $ sign in the name. You can fork the code.&lt;br /&gt;
&lt;b&gt;Tested for&lt;/b&gt; JDK 1.5 and 1.6 on SUSE Linux in ksh, csh and bash shells.&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;background-color: #ffd966;&quot;&gt;&lt;b&gt;Download link:&lt;/b&gt;&lt;/span&gt;&amp;nbsp;&lt;a href=&quot;http://ideone.com/plain/W7xM03&quot;&gt;http://ideone.com/plain/W7xM03&lt;/a&gt;&lt;/blockquote&gt;
The script: &lt;b&gt;JarSearch.sh&lt;/b&gt;&lt;br /&gt;
&lt;!--&lt;script src=&quot;http://ideone.com/e.js/W7xM03&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;--&gt;

&lt;br /&gt;
&lt;pre class=&quot;brush:shell&quot;&gt;#!/bin/sh

className=$1
JAR=$JAVA_HOME/bin/jar
JARFILES=`find . -name &quot;*jar&quot;`
found=0
ask=1

#########################################
#Author: Rajdeep Biswas
#Created: 2014-04-30
#Website: www.javabambino.blogspot.com
#Known issue: Not for inner classes
#########################################
echo &quot;&quot;
echo &quot;*****************************************&quot;
echo &quot;About: Searches .jar in current path (sub-directories inclusive) for a specified class file.&quot;
echo &quot;Usage: $0 class_name&quot;
echo &quot;Example: $0 HashMap&quot;
echo &quot;Note: Give exact class name without .class extension.&quot;
echo &quot;*****************************************&quot;
echo &quot;&quot;

#Error if there is no or multiple arguments
if [ $# -ne 1 ]
then
 echo &quot;ERROR: Wrong number of arguments specified, please check usage above!&quot;
 exit 1;
fi

#Ask how to search
while [ $ask -eq 1 ]
do
 echo &quot;1. Enter &#39;1&#39; to search for exactly $className.class&quot;
 echo &quot;2. Enter &#39;2&#39; to search for *$className.class pattern. For example, Sample$className.class&quot;
 echo &quot;3. Enter &#39;0&#39; to exit this program&quot;
 read temp
 
#validate input is only 1, 2 or 0. If not, take input again.
 if [[ -n ${temp//[0-2]/} ]]; 
 then
  echo -e &quot;Caution: Invalid input!\n&quot;
  continue
 fi

 if [ $temp -eq 0 ]
 then
  echo &quot;Exiting..&quot;
  exit 1
 fi

 if [ $temp -eq 1 -o $temp -eq 2 ]
 then
  searchHow=$temp
  ask=0
 fi
done

#Set the grep pattern accordingly as user input on how to search
if [ $searchHow -eq 1 ]
then
 searchWhat=&quot;grep -w $className.class&quot;
elif [ $searchHow -eq 2 ]
then
 searchWhat=&quot;grep $className.class&quot;
fi

echo -e &quot;Scanning started...\n&quot;
#Start scan
for JARFILE in $JARFILES
do
 $JAR tvf $JARFILE | $searchWhat  &amp;gt; /dev/null
 
 if [ $? -eq 0 ]
 then
  found=1
  echo &quot;==&amp;gt; Found in $JARFILE&quot;
 fi
done
#End scan

if [ $found -eq 0 ]
then
 echo &quot;$className.class not found in current path: &quot;`pwd`
fi

echo -e &quot;\nScanning over.&quot;
&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/3566596499616425829/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2014/04/finding-jar-files-which-contain.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/3566596499616425829'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/3566596499616425829'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2014/04/finding-jar-files-which-contain.html' title='Finding the JAR files which contain a specific .class file on Linux machine'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVdl7TcSsWOi1Ut99nSdyShhQYGcT7Om9jEH1g66vlYISltHY4VioGWEbupAiVuGXFB5OLuQ8Jw8FOly8R6Z-aO-rRifHppyZdUxKKjapW76fCfaG-pbIAYY23q5GOJUOEWKhpHL5K-i0G/s72-c/java_linux.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-6671191895702587607</id><published>2014-04-24T16:29:00.000+05:30</published><updated>2014-04-28T11:45:49.118+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="locking"/><category scheme="http://www.blogger.com/atom/ns#" term="Multi-threading"/><category scheme="http://www.blogger.com/atom/ns#" term="synchronization"/><title type='text'>Synchronization in Java : class and object locking concept</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
People talk about two types of multi-threaded locking - object and class. In my knowledge, locking is done on objects only. Let me explain.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Case 1: On objects we create&lt;/b&gt; using new or factory methods etc.
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;void synchronized myMethod(Type param) {
  //will lock on the instance used to call this method
}&lt;/pre&gt;
or
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;synchronized(this) {
 //will lock on current object
}&lt;/pre&gt;
or
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;synchronized(obj1) {
 //will lock on specified obj1 object
}&lt;/pre&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;b&gt;Case 2: On java.lang.Class objects&lt;/b&gt;
This is called class lock, and can be used with static fields or methods or blocks, as they belong to class and shared among all the objects, and other class properties.&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;static void synchronized method() {
  //will lock the Class object
}&lt;/pre&gt;
or
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;static {
  synchronized(SomeClass.class){
    int a = 2;
  }
}&lt;/pre&gt;
This is also object locking because &lt;a href=&quot;http://javabambino.blogspot.com/2013/01/jvm-runtime-data-areas-part-12.html&quot; target=&quot;_blank&quot; ref=&quot;nofollow&quot;&gt;classes are loaded into the Method Area in the JVM&lt;/a&gt;, and all the static members of the class are wrapped inside a &lt;tt&gt;java.lang.Class&lt;/tt&gt; object created by JVM. So behind abstraction, its that class&#39; object locking and in the front picture, we consider it class locking.
&lt;br /&gt;
So I can also infer one more thing. Just as objects locked by a thread can not be acquired by another thread as long as it is not released by first thread, class locking (the &lt;tt&gt;java.lang.Class&lt;/tt&gt; instance) also works in same manner as it is essentially object locking only.
&lt;br /&gt;
&lt;br /&gt;
Now slightly digging into the class locking concept. Taking the following class hierarchy.
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;class Animal {
 static synchronized void eat() {
  //generic eating habits
 }
}

class Dog extends Animal implements Runnable{
 public void run() {
  Dog.eat();
 }
}&lt;/pre&gt;
The above code will result into locking &lt;tt&gt;Animal.class&lt;/tt&gt; object only whether we call &lt;tt&gt;eat()&lt;/tt&gt; method using &lt;tt&gt;Dog&lt;/tt&gt; or &lt;tt&gt;Animal&lt;/tt&gt;, because &lt;tt&gt;eat()&lt;/tt&gt; is present in only &lt;tt&gt;Animal&lt;/tt&gt; class.
&lt;br /&gt;
Let&#39;s change the scenario.
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;class Animal {
 static synchronized void eat() {
  //generic eating habits
 }
}

class Dog extends Animal implements Runnable{
 public void run() {
  Dog.eat();
 }
 static synchronized void eat() {
  //doggy eating habits
 }
}&lt;/pre&gt;
Now that &lt;tt&gt;eat()&lt;/tt&gt; method is present in both classes, lock will be acquired only on that class on which the method was called. &lt;tt&gt;Dog.class&lt;/tt&gt; in above case. If it was &lt;tt&gt;Animal.eat()&lt;/tt&gt;, will lock &lt;tt&gt;Animal.class&lt;/tt&gt;.
&lt;br /&gt;
Lets do a little bit more.
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;class Animal {
 static synchronized void eat() {
  //generic eating habits
 }
}

class Dog extends Animal implements Runnable{
 public void run() {
  Dog.eat();
 }
 static synchronized void eat() {
  super.eat();
 }
}&lt;/pre&gt;
When started, the &lt;tt&gt;Dog&lt;/tt&gt; thread will go on to lock the &lt;tt&gt;Dog.class&lt;/tt&gt; and then &lt;tt&gt;Animal.class&lt;/tt&gt;.
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Note:&lt;/b&gt;&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;tt&gt;synchronized&lt;/tt&gt; keyword helps in mutual exclusion, so when a lock is acquired on any object by a thread, only the synchronized properties are kept safe from manipulation by other threads, the non-synchronized properties like variables or methods etc, are free to get manipulated.&lt;/li&gt;
&lt;li&gt;Each object has a synchronization lock, which can be owned by only one thread at a time. However, a thread can own multiple locks at the same time.&lt;/li&gt;
&lt;li&gt;Synchronization can help to eliminate race conditions but can introduce deadlock into the application..&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/6671191895702587607/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2014/04/synchronization-in-java-class-and.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/6671191895702587607'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/6671191895702587607'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2014/04/synchronization-in-java-class-and.html' title='Synchronization in Java : class and object locking concept'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-2934989771609001963</id><published>2014-04-21T18:21:00.001+05:30</published><updated>2016-02-02T15:37:21.550+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Design patterns"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="singleton"/><title type='text'>Writing singleton classes in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Singleton classes are those classes which can be instantiated only once&lt;/b&gt;. We need to &lt;i&gt;restrict the creation of multiple instances&lt;/i&gt; of that class by blocking constructor access using &lt;tt&gt;new&lt;/tt&gt; keyword etc, and instead regulate the incoming instance requests by creating the instance only once and return the same instance time and again on multiple calls.
&lt;br /&gt;
&lt;b&gt;Where do we actually need this singleton design pattern?&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;The answer will be where you need only one instance and not multiple instances of a class across your application. This does not degrades the performance since your work needs only single instance and using this design pattern you are preventing multiple instances. The actual case might just vary according to your needs. This may be logging, or database connection object. But you have to decide:&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;will you ever need exactly one instance in your application?&lt;/li&gt;
&lt;li&gt;is your application expecting the exactly same implementation of this class?&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
among many other factors before you finally settle in to make a class singleton. An insightful reading at the developerWorks&amp;nbsp;website can be found &lt;a href=&quot;https://www.ibm.com/developerworks/library/co-single/&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;here&lt;/a&gt;.&lt;/div&gt;
&lt;br /&gt;
There are many ways, some ways are presented here as different code snippets. Let me know if you have a better idea and see a flaw in these codes that I may have missed.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Code snippet 1:&lt;/b&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package designpattern;
/**
 * Early instantiation means that the singleton instance 
 * is created during class loading itself and not when 
 * first used. So, no synchronization is required by using a static field.
 * This means that whether or not we require the singleton instance, 
 * it will be created and the object will be unique.
 * Works well in singlethreaded and multithreaded environments.
 * 
 * @author xploreraj
 */
class Singleton1 {
 private static Singleton1 instance = new Singleton1();
 
 private Singleton1() {
  //disable outside access
 }
 
 public static Singleton1 getInstance(){
  return instance;
 }
}


/*
 * test
 */
public class TestSingleton1 implements Runnable {
 
 public static void main(String[] args) {
  int count = 100;
  Thread thread[] = new Thread[count];
  for(int i=0; i&amp;lt; count; i++){
   thread[i] = new Thread(new TestSingleton1());
  }
  for(int j=0; j&amp;lt; count; j++){
   thread[j].start();
  }
  
 }

 @Override
 public void run() {
  Singleton1 instance = Singleton1.getInstance();
  System.out.println(&quot;Accessed by &quot; + Thread.currentThread().getName() +
    &quot;; Singleton1 instance is &quot; + instance.hashCode());
  
 }
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Code snippet 2:&lt;/b&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package designpattern;
/**
 * Lazy instantiation of singleton using synchronization 
 * on getInstance() method makes it is usable in 
 * multithreaded environments.
 * 


 * The instance will be null only the first time, and 
 * then it will always be not null so that the singleton 
 * instance is returned directly. We need synchronization 
 * for the first time so that multiple threads 
 * concurrently do not get inside the getInstance() method 
 * while instance == null and then create multiple singleton instances.
 * But using synchronization on the method will unnecessarily 
 * take computation resources by locking the method everytime. 
 * Since instance is not null and getInstance() should return the instance directly.
 * 
 * @author xploreraj
 *
 */
class Singleton2 {
 private static Singleton2 instance;
 private Singleton2() {
  //hide from outside
 }

 public static synchronized Singleton2 getInstance() {

  if(instance==null) {
   for(int i=0; i&amp;lt; 100000; i++)
    ;

   instance = new Singleton2();
  }
  return instance;
 }//getInstance()
}

/*
 * test
 */
public class TestSingleton2 implements Runnable{

 public static void main(String[] args) {
  int count = 20;
  Thread thread[] = new Thread[count];
  for(int i=0; i&amp;lt; count; i++){
   thread[i] = new Thread(new TestSingleton2());
  }
  for(int j=0; j&amp;lt; count; j++){
   thread[j].start();
  }
  
 }//main()

 @Override
 public void run() {
  Singleton2 instance = Singleton2.getInstance();
  System.out.println(&quot;Accessed by &quot; + Thread.currentThread().getName() +
    &quot;; Singleton2 instance is &quot; + instance.hashCode());
  
 }//run()

}
&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Code snippet 3:&lt;/b&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package designpattern;
/**
 * clever but DEFECTIVE SINGLETON
 * Lazy instantiation of the singleton.
 * 

 * Checking the instance variable for null and then 
 * applying synchronization improves the code
 * at Singleton2 class, because if instance is 
 * not null, i.e., after first execution of the code,
 * the control will never get inside the synchronized 
 * block and computation cost will be saved. 

 * But there will be a problem. 
 * Say Thread1 checks instance = null and gets inside
 * the block but is preempted by Thread2 before executing 
 * the synchronized block. Now instance being null
 * Thread2 will also enter the block and for each of those 
 * threads, different instances will be created.
 * SO SINGLETON WILL NOT BE POSSIBLE IN MULTITHREADED ENVIRONMENT.
 * 
 * @author xploreraj
 *
 */
class Singleton3 {
 private static Singleton3 instance;
 private Singleton3() {
  //to do
 }
 
 public static Singleton3 getInstance() {
  if (instance==null){
   
   for(int j = 0; j&amp;lt; 100000;j++)
    //lets say that a thread is involved in something 
    //after coming inside this null checked block
    ;   
   
   synchronized(Singleton3.class){
     instance = new Singleton3();
   }
  }
  
  return instance;
 }
}

public class TestSingleton3 implements Runnable {

 public static void main(String[] args) {
  int count = 20;
  Thread thread[] = new Thread[count];
  for(int i=0; i&amp;lt; count; i++){
   thread[i] = new Thread(new TestSingleton3());
  }
  for(int j=0; j&amp;lt; count; j++){
   thread[j].start();
  }
  
 }//main()

 @Override
 public void run() {
  Singleton3 instance = Singleton3.getInstance();
  System.out.println(&quot;Accessed by &quot; + Thread.currentThread().getName() +
    &quot;; Singleton3 instance is &quot; + instance.hashCode());
  
 }//run()

}
&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Code snippet 4:&lt;/b&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package designpattern;
/**
 * Lazy instantiation of the singleton.
 * 

 * This is double-checked locking example and will prevent 
 * following scenario from breaking the singleton policy.

 * &lt;b&gt;Description:&lt;/b&gt;

 * 1. Thread-1 enters getInstance() method for first time 
 * since instance == null, and then is preempted by Thread-2.

 * 2. Thread-2 enters getInstance() since instance is still null. 
 * It goes inside the inner synchronized block and 
 * once more instance == null check is done. It is null so 
 * instance has a new object now.

 * 3. Thread-2 is preempted by Thread-1, and now Thread-1 enters the 
 * internal synchronized block. Since an additional
 * check for instance == null is made, and this time 
 * instance is non-null, it will not reach the code for fresh
 * initialization.

 * &lt;b&gt;Note:&lt;/b&gt;

 * There is a memory problem known as Out of Order writes in older JVM,
 * meaning a reference to an object could be returned by JVM just by
 * allocation memory and making the reference but before the construction,
 * precisely constructor logic, is actually over, so returning a partially
 * created object to one thread which cou,d lead to a lot of discrepancies.
 * Ref: http://www.javaworld.com/article/2074979/java-concurrency/double-checked-locking--clever--but-broken.html
 * 
 * @author xploreraj
 *
 */
class Singleton4 {
 private static Singleton4 instance;
 private Singleton4() {
  //to do
 }
 
 public static Singleton4 getInstance() {
  if (instance==null){
   
   for(int j = 0; j&amp;lt; 100000;j++) 
    //lets say that a thread is involved in something 
    //after coming inside this null checked block
    ;
   
   synchronized(Singleton4.class){
    if (instance==null){
     instance = new Singleton4();
    }
   }
  } 
  return instance;
 }
}

public class TestSingleton4 implements Runnable {

 public static void main(String[] args) {
  int count = 20;
  Thread thread[] = new Thread[count];
  for(int i=0; i&amp;lt; count; i++){
   thread[i] = new Thread(new TestSingleton4());
  }
  for(int j=0; j&amp;lt; count; j++){
   thread[j].start();
  }
  
 }//main()

 @Override
 public void run() {
  Singleton4 instance = Singleton4.getInstance();
  System.out.println(&quot;Accessed by &quot; + Thread.currentThread().getName() +
    &quot;; Singleton4 instance is &quot; + instance.hashCode());
  
 }//run()

}
&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Code snippet 5:&lt;/b&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package designpattern;

/**
 * enums are by default singleton, inherently serializable, not clonable, with final fields
 * @author Rajdeep
 *
 */
enum EnumSingleton {
 INSTANCE;
 
 private EnumSingleton(){
  //can initialize something internally
 }
 
 public void doSomething(){
  //do something using same instance
 }
}

public class BestSingleton extends Thread{

 public static void main(String[] args) {
  int count = 100;
  Thread thread[] = new Thread[count];
  for(int i=0; i&amp;lt; count; i++){
   thread[i] = new Thread(new BestSingleton());
  }
  for(int j=0; j&amp;lt; count; j++){
   thread[j].start();
  }
  
 }//main()

 @Override
 public void run() {
  EnumSingleton instance = EnumSingleton.INSTANCE;
  System.out.println(&quot;Accessed by &quot; + Thread.currentThread().getName() +
    &quot;; EnumSingleton instance hashcode is &quot; + instance.hashCode());
  
 }//run()

}
&lt;/pre&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/2934989771609001963/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2014/04/writing-singleton-classes-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/2934989771609001963'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/2934989771609001963'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2014/04/writing-singleton-classes-in-java.html' title='Writing singleton classes in Java'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-8303032748757386361</id><published>2014-04-11T18:57:00.000+05:30</published><updated>2016-02-13T19:48:08.267+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Bubble Sort"/><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Sorting"/><title type='text'>Bubble Sorting in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Bubble sorting sorts the array in-place bubbling up the largest value.&lt;br /&gt;
Complexity:&lt;br /&gt;
Worst case - O(n^2); average case - O(n^2); best case - O(n)&lt;br /&gt;
&lt;br /&gt;
Visualization: (src. Wiki)&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://upload.wikimedia.org/wikipedia/commons/c/c8/Bubble-sort-example-300px.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://upload.wikimedia.org/wikipedia/commons/c/c8/Bubble-sort-example-300px.gif&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package org.algorithms.sorting;
import java.util.Arrays;

public class BubbleSortDemo {
public static void doBubbleSort(int[] args) {
        boolean swapped;
        do {
            swapped = false;
            for (int i=1; i&amp;lt;args.length; i++)
                if (args[i-1] &amp;gt; args[i]) {
                    int temp = args[i - 1];
                    args[i - 1] = args[i];
                    args[i] = temp;
                    swapped = true;
                }
        } while(swapped);
    }

    public static void main(String[] args) {
        int[] numbers = {5,4,3,2,1,6};
        System.out.println(&quot;Original: &quot; + Arrays.toString(numbers));
        doBubbleSort(numbers);
        System.out.println(&quot;Sorted: &quot; + Arrays.toString(numbers));
    }

}
&lt;/pre&gt;
&lt;br /&gt;
Output:&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
Original: [5, 4, 3, 2, 1, 6]&lt;br /&gt;
Sorted: [1, 2, 3, 4, 5, 6]&lt;/blockquote&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/8303032748757386361/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2014/04/bubble-sorting-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/8303032748757386361'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/8303032748757386361'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2014/04/bubble-sorting-in-java.html' title='Bubble Sorting in Java'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-2891400937900510155</id><published>2014-03-20T16:15:00.001+05:30</published><updated>2014-03-26T18:06:02.664+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><title type='text'>Fibonacci Series in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Q. Print Fibonacci series in Java.&lt;br/&gt;
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55

&lt;pre class=&quot;brush:java&quot;&gt;
public class FibonacciSeries {
  
 public static void main(String[] args) {
  int sum = 0;
  //how many numbers to print
  for(int i=0, j=1; i&lt;=10; i++){
   if(i==0){
    //for printing first number which is 0
    System.out.print(0 + &quot;, &quot;);
   }
   else{
    //j has last &#39;sum&#39;, and sum now stores latest added value
    //while the control enters again, j gets latest &#39;sum&#39; value before
    //new sum is calculated and printed again
    sum = j + (j=sum);
    System.out.print(sum + &quot;, &quot;);
   }
  }
 }
}
&lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/2891400937900510155/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2014/03/fibonacci-series-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/2891400937900510155'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/2891400937900510155'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2014/03/fibonacci-series-in-java.html' title='Fibonacci Series in Java'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-3612859887794333707</id><published>2014-01-30T11:20:00.001+05:30</published><updated>2014-01-30T23:13:23.222+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Matrix"/><title type='text'>Matrix Multiplication in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package com.learning.ds.matrices;
import com.learning.ds.arrays.ArrayUtil;

/**
 * Created by Rajdeep on 28/1/14.
 */
public class MatrixMultiplicationDemo {
    public static void main(String[] args){
        //the temp1 and temp2 array values can be changed to test this program
        int[][] temp1 = {{2,0}};
        int[][] temp2 = {{1,1,1}, {2,2,2}};

        System.out.println(&quot;Matrix 1: &quot;);
        ArrayUtil.print2DIntArray(temp1);
        System.out.println(&quot;Matrix 2: &quot;);
        ArrayUtil.print2DIntArray(temp2);

        if( !( isFilledMatrix(temp1) &amp;amp;&amp;amp; isFilledMatrix(temp2) ) ){
            System.out.println(&quot;One of the matrices is not filled completely and can&#39;t be used for multiplication.&quot;);
        }
        else{
            //check dimension
            if(!areMatricesMultipliable(temp1, temp2)){
                System.out.println(&quot;The number of columns in first matrix is unequal to number of rows in second matrix. The must be equal for dot product.&quot;);
            }
            else{
                System.out.println(&quot;can be multiplied&quot;);
                int[][] tempNew = multiplyMatrices(temp1, temp2);
                System.out.println(&quot;new matrix is: &quot;);
                ArrayUtil.print2DIntArray(tempNew);
            }
        }
    }

    private static int[][] multiplyMatrices(int[][] temp1, int[][] temp2) {
        //
        int rowCount1 = temp1.length;
        int colCount1 = temp1[0].length;
        int colCount2 = temp2[0].length;

        int[][] tempNew = new int[rowCount1][colCount2];

        for(int i=0; i &amp;lt; rowCount1; i++){
            int k=0;
            while(k &amp;lt; colCount2){
                int tempVal = 0;
                for (int j=0; j &amp;lt; colCount1; j++){
                    tempVal = tempVal + ( temp1[i][j] * temp2[j][k] );
                }
                tempNew[i][k] = tempVal;
                k++;
            }

        }

        return tempNew;
    }

    //matrix multiplication rule is
    //no of columns of left matrix == no of rows in right matrix
    // (m x n) . (n x p) = (m x p)
    private static boolean areMatricesMultipliable(int[][] mat1, int[][] mat2){
        return (mat1[0].length == mat2.length);
    }

    //check the matrix has all elements filled or not
    //In other words, checking whether number of columns in each row is same
    private static boolean isFilledMatrix(int[][] mat){
        boolean retVal = false;
        int rows = mat.length;
        
        if(rows == 1){
            retVal = true;
        }
        else {
            for(int i=0; i &amp;lt; (rows-1); i++){
                if(mat[i].length != mat[i+1].length){
                    retVal = false;
                    break;
                }
                else
                    retVal = true;
            }
        }

        return retVal;
    }
}

&lt;/pre&gt;
&lt;br /&gt;
The &lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;ArrayUtil.print2DArray(int[][] args)&lt;/pre&gt;
prints the array on screen through normal &#39;for&#39; loops.&lt;br /&gt;
One sample output:&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Matrix 1:&lt;br /&gt;2 0&lt;br /&gt;Matrix 2:&lt;br /&gt;1 1 1&lt;br /&gt;2 2 2&lt;br /&gt;can be multiplied&lt;br /&gt;new matrix is:&lt;br /&gt;2 2 2&lt;/span&gt;&lt;/blockquote&gt;
Another sample output by changing the temp1 and temp2 arrays in main() method:&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Matrix 1::&lt;br /&gt;2 0 3:&lt;br /&gt;Matrix 2::&lt;br /&gt;1 1 1:&lt;br /&gt;2 2 2:&lt;br /&gt;One of the matrices is not filled completely and can&#39;t be used for multiplication.&lt;/span&gt;&lt;/blockquote&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/3612859887794333707/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2014/01/matrix-multiplication-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/3612859887794333707'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/3612859887794333707'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2014/01/matrix-multiplication-in-java.html' title='Matrix Multiplication in Java'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-8645712982689712315</id><published>2014-01-27T18:44:00.001+05:30</published><updated>2014-02-04T23:41:44.519+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Matrix"/><title type='text'>Matrix Operation in Java: Addition of two 2x2 matrices demonstrated</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;tr_bq&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
For addition or subtraction, the dimensions of the matrices must be same. This is just a demonstration code and can be rewritten in a far more optimized manner.&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package com.learning.ds.matrices;

/**
 * Created by Rajdeep on 27/1/14.
 */
public class MatrixAdditionDemo {
    public static void main(String[] args){
        int[][] arr1 = {{1,2,3},{4,2,6}};
        int[][] arr2 = {{10,20,30},{40,50,60}};

        System.out.println(&quot;Matrix A:&quot;);
        for(int i=0; i &amp;lt; arr1.length; i++){
            for(int j=0; j &amp;lt; arr1[i].length; j++){
                System.out.print(arr1[i][j] + &quot; &quot;);
            }
            System.out.println();
        }

        System.out.println(&quot;Matrix B:&quot;);
        for(int i=0; i &amp;lt; arr2.length; i++){
            for(int j=0; j &amp;lt; arr2[i].length; j++){
                System.out.print(arr2[i][j] + &quot; &quot;);
            }
            System.out.println();
        }

        System.out.println(&quot;Addition of A and B:&quot;);
        int[][] tempArr = addMatrices(arr1,arr2);
        if(tempArr!=null){
            for(int i=0; i &amp;lt; tempArr.length; i++){
                for(int j=0; j &amp;lt; tempArr[i].length; j++){
                    System.out.print(tempArr[i][j] + &quot; &quot;);
                }
                System.out.println();
            }
        }

    }

    //add the matrices
    private static int[][] addMatrices(int[][] m1, int[][] m2){
        //check size
        if(!ofSameSize(m1, m2)){
            System.out.println(&quot;[Error] The matrices&#39; dimension in operation do not match!&quot;);
            return null;
        }
        int len1 = m1.length;
        int[][] tempArr = new int[2][3];
        for(int i=0; i &amp;lt; tempArr.length; i++){
            for(int j=0; j &amp;lt; tempArr[i].length; j++){
                tempArr[i][j] = m1[i][j] + m2[i][j];
            }
        }
        return tempArr;
    }

    //for adding, the size must be same
    private static boolean ofSameSize(int[][] m1, int[][] m2) {
        int len1 = m1.length;
        int len2 = m2.length;
        boolean check = true;
        if(len1==len2 &amp;amp; len1!=0){
            //System.out.println(&quot;length is: &quot; + len1 + &quot; and &quot; + len2);
            for(int i=0; i &amp;lt; len1; i++){
                if(!(m1[i].length == m2[i].length)){
                    check = false;
                }
            }
        }
        else
            check = false;

        return check;
    }
}

&lt;/pre&gt;
Sample output 1:&lt;br /&gt;
&lt;blockquote&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Matrix A:&lt;br /&gt;1 2 3&lt;br /&gt;4 2 6&lt;/span&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Matrix B:&lt;br /&gt;10 20 30&lt;/span&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Addition of A and B:&lt;br /&gt;[Error] The matrices&#39; dimension in operation do not match!&lt;/span&gt;&lt;/blockquote&gt;
&lt;br /&gt;
Sample output 2:&lt;br /&gt;
&lt;blockquote&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Matrix A:&lt;br /&gt;1 2 3&lt;br /&gt;4 2 0&amp;nbsp;&lt;/span&gt;&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Matrix B:&lt;br /&gt;10 20 30&lt;br /&gt;11 5 3&amp;nbsp;&lt;/span&gt;&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Addition of A and B:&lt;br /&gt;11 22 33&lt;br /&gt;15 7 3&amp;nbsp;&lt;/span&gt;&lt;/blockquote&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/8645712982689712315/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2014/01/matrix-operation-in-java-addition-of.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/8645712982689712315'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/8645712982689712315'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2014/01/matrix-operation-in-java-addition-of.html' title='Matrix Operation in Java: Addition of two 2x2 matrices demonstrated'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-1442497884194583605</id><published>2014-01-27T17:20:00.000+05:30</published><updated>2016-02-13T23:03:22.333+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Quick Sort"/><category scheme="http://www.blogger.com/atom/ns#" term="Sorting"/><title type='text'>Quick Sort Demo</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
This is a divide and conquer approach. Take a pivot as the last element of array, and place all numbers lesser than this to the left. This is done using partitioning as seen in the code.&lt;br /&gt;
Complexity:&lt;br /&gt;
Best - O(n logn); Average - O(n logn); Worst - O(n^2)&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;!--&lt;script src=&quot;http://ideone.com/e.js/iOVK83&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;--&gt;

&lt;pre class=&quot;brush:java&quot;&gt;package org.algorithms.sorting;

import java.util.Arrays;

public class QuickSortDemo {

    public static void main(String...args){
        int[] arr = {4,5,3,2,1,7};
        System.out.println(&quot;initial array:&quot;+ Arrays.toString(arr));
        quickSort(arr, 0, arr.length-1);
        System.out.println(&quot;\nfinal array:&quot;+Arrays.toString(arr));
    }

    /**
     * Applies the quicksort
     * @param arr The array to be sorted
     * @param start Starting index for sorting
     * @param end End index for sorting
     */
    private static void quickSort(int[] arr, int start, int end) {
        if(start &amp;lt; end) {
            int pIndex = partition(arr,start, end);
            quickSort(arr, start, pIndex-1);
            quickSort(arr, pIndex+1, end);
        }
    }

    /**
     * Push all elements lesser than or equal pivot to left
     * of partition index, and finally, replace pivot with
     * element at partition index, so that we have the sub-array
     * from partition index towards left sorted in ascending fashion.
     * @param start index
     * @param end index
     * @return partition index
     */
    private static int partition(int[] arr, int start, int end) {
        int pivot = arr[end];   //pivot, last element here
        int pIndex = start;       //partition index
        for(int i = start; i&amp;lt;= (end-1); i++)
            if(arr[i] &amp;lt; pivot) {
                swap(arr, i,pIndex);
                pIndex++;
            }
        //now that all elements left to pIndex are lesser
        // than pivot (which is at end until now),
        //swap the pIndex element and pivot, so that
        // the pivot is in correct place now
        swap(arr, pIndex, end);
        return pIndex;
    }

    /**
     * Swaps the elements in the &#39;arr&#39; array on specified indices
     * @param index1
     * @param index2
     */
    private static void swap(int[] arr, int index1, int index2) {
        int temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
    }

}
&lt;/pre&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/1442497884194583605/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2014/01/quick-sort-demo.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/1442497884194583605'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/1442497884194583605'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2014/01/quick-sort-demo.html' title='Quick Sort Demo'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total><georss:featurename>Bengaluru</georss:featurename><georss:point>12.9715987 77.594562699999983</georss:point><georss:box>12.4764182 76.949115699999979 13.4667792 78.240009699999987</georss:box></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-5940998318159890933</id><published>2013-12-30T17:28:00.001+05:30</published><updated>2013-12-30T17:29:23.629+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Personal Note"/><title type='text'>Learning Data Structures now</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Nowadays, I am learning data structures starting with simple sorting techniques, searches, linked-list, stack algorithms etc, and I will post about my experiences. I don&#39;t know how far, but I&#39;ll share so far I learn. :-)&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/5940998318159890933/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2013/12/learning-data-structures-now.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/5940998318159890933'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/5940998318159890933'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2013/12/learning-data-structures-now.html' title='Learning Data Structures now'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-8683531053276069431</id><published>2013-12-30T13:26:00.000+05:30</published><updated>2016-02-14T02:17:37.258+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><category scheme="http://www.blogger.com/atom/ns#" term="InsertionSort"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="Sorting"/><title type='text'>Insertion Sorting Logic</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;i&gt;Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain. -Wiki&lt;/i&gt;&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
The insertion sorting logic is as follows:

An array with one element is already sorted. For multiple elements, take an element starting from index 1 into temp variable, and sort its left, then place the temp to its correct location.&lt;br /&gt;
Now take 2nd element into temp variable, and sort its indices 0, 1 and 2. Put value at temp to appropriate location.&lt;br /&gt;
We are looking left of the pivot element that we have stored in temp variable in each iteration, while gradually moving towards right direction while sorting the portion of the array from the pivot to the beginning. The appropriate location is found when do not find a value in the left side of array which can be sorted after comparing with temp, so we put temp there.&lt;br /&gt;
&lt;br /&gt;
Complexity:&lt;br /&gt;
Best - O(n), average - O(n^2), worst - O(n^2)&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package org.algorithms.sorting;

import java.util.Arrays;

public class InsertionSortDemo {
    public static void main(String[] args) {
        int[] numbers = {5,4,3,2,1};
        System.out.println(Arrays.toString(numbers));
        
        if (numbers.length &amp;gt; 1)
            doInsertionSort(numbers);
        
        System.out.println(Arrays.toString(numbers));
    }

    public static void doInsertionSort(int[] input){
        for (int i = 1; i &lt; input.length; i++) {
            int j = i;
            //does not runs for sorted array, leaving best case to O(n)
            while (j&gt;0 &amp;&amp; input[j-1] &gt; input[j]) {
                swap(input, j, j - 1);
                j--;
            }
            //O(n^2) even for sorted array
            /*for(int j = i ; j &gt; 0 ; j--) {
                System.out.println(&quot;ran &quot;);
                if(input[j] &lt; input[j-1])
                    swap(input,j,j-1);
                    }*/
        }
    }

    private static void swap(int[] input, int i, int j) {
        int temp = input[i];
        input[i] = input[j];
        input[j] = temp;
    }
}

&lt;/pre&gt;
&lt;span style=&quot;background-color: yellow;&quot;&gt;Visualization:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
5 4 3 2 1 (in)&lt;br /&gt;
&lt;br /&gt;
i=1&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;j=1 :: 4 5 3 2 1&lt;br /&gt;
i=2&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;j=2 :: 4 3 5 2 1&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;j=1 :: 3 4 5 2 1&lt;br /&gt;
i=3&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;j=3 :: 3 4 2 5 1&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;j=2 :: 3 2 4 5 1&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;j=1 :: 2 3 4 5 1&lt;br /&gt;
i=4&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;j=4 :: 2 3 4 1 5&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;j=3 :: 2 3 1 4 5&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;j=2 :: 2 1 3 4 5&lt;br /&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;j=1 :: 1 2 3 4 5&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/8683531053276069431/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2013/12/insertion-sorting-logic.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/8683531053276069431'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/8683531053276069431'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2013/12/insertion-sorting-logic.html' title='Insertion Sorting Logic'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-2949162649442486316</id><published>2013-12-16T01:20:00.000+05:30</published><updated>2013-12-16T11:44:11.944+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Data Structure"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="LinkedList"/><title type='text'>LinkedList: Node creation and display example</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;package ds;

/**
 * @author Rajdeep
 * 
 * Note
 * Creating the list:
 * 1. &#39;start&#39; refers to the first node and &#39;now&#39; is made to refer to the latest
 *   added node.
 * 2. after creating the first linkedlist object and assigning value,
 *   &#39;start&#39; points to it because we are checking for start==null
 *   &#39;start&#39; is null in first node addition only.
 *   &#39;now&#39; also points to it, this being the latest node.
 * 3. Second node onwards, start is never null, so control goes to else part.
 *   &#39;now.next&#39; points to nothing because its of reference type.
 *   so assign &#39;now.next&#39; to the newly created object after
 *   checking &#39;now.next==null&#39;.
 * 4. Now &#39;start&#39; pointed or the first object&#39;s (node&#39;s) &#39;next&#39; part is linked
 *   to the 2nd object since now was referring to it, and then after
 *   &#39;while&#39; execution, &#39;now&#39; points to the newly created node, referred by
 *   the local &#39;temp&#39; variable.
 * 5. e.g.: start -&amp;gt; [10|add1] at add0 -&amp;gt; [20|add2] at add1 -&amp;gt; [30|NULL] at add2;
 *   &#39;now&#39; points to this add2 having info 30, and &#39;now.next&#39; is NULL,
 *   so that when another node is created, &#39;now.next&#39; refers to the new
 *   node, and then &#39;now&#39; refers to the new node, and so on.
 * 
 * Displaying the list:
 * We start from the starting node, and display &#39;info&#39; part of each node, and
 * move on to next node by checking the current node&#39;s &#39;next&#39; part is not null.
 */

// This is the Linked List node
class LinkedList{
 int info;   //has value
 LinkedList next; //for pointing to next node
}

// Demonstration on creation of node and displaying the values
public class LinkedListDemo {

 static LinkedList start = null;
 static LinkedList now = null;
 
 public static void main(String[] args) {
  createNode(0);
  createNode(1);
  createNode(5);
  createNode(7);
  createNode(10);
  createNode(11);
  createNode(54);
  createNode(76);
  
  display();
 }
 
 static void createNode(int val){
  LinkedList temp = new LinkedList();
  
  temp.info = val;
  if (start==null){
   start = temp;
   now = temp;
  }
  else{
   while(now.next==null){
    now.next=temp; 
   }
   now = temp;
  }
 }
 
 static void display(){
  LinkedList temp = start;
  System.out.print(&quot;[&quot;);
  while(temp!=null){
   System.out.print(temp.info + &quot;|&quot;);
   temp = temp.next;
  }
  System.out.println(&quot;\b]&quot;);
 }

}
&lt;/pre&gt;
The output will be:&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;&quot;&gt;[0|1|5|7|10|11|54|76]&lt;/span&gt;
&lt;/blockquote&gt;
This was a demo program for my understanding, and now I understand how the nodes are connected, and so the term &quot;linked list&quot;.&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/2949162649442486316/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2013/12/linkedlist-node-creation-and-display.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/2949162649442486316'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/2949162649442486316'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2013/12/linkedlist-node-creation-and-display.html' title='LinkedList: Node creation and display example'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3781349183261963248.post-1639609451193159552</id><published>2013-10-01T11:32:00.000+05:30</published><updated>2013-12-16T01:52:27.550+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Exception and Error"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="try-catch-finally"/><title type='text'>The try-catch-finally love affair!</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
Ok, everybody here already know about the affair, I am just discussing some more bits of this love triangle.&lt;br /&gt;
Lets dig hard to the shallow level :)
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;try{
    //try something
}
//and if the trial goes wrong, catch the issue and handle
catch(Exception e){
    //handling the problematic part
    //or at least letting the program to continue
    //and exit gracefully and not shut at face!
}
//but
finally{
    //do this, so you are the winner!!
}
&lt;/pre&gt;
&lt;br /&gt;
So from even the baddest of our common-sense, we smell that &lt;tt&gt;finally&lt;/tt&gt; block supercedes the two others, viz., &lt;tt&gt;try&lt;/tt&gt; and &lt;tt&gt;catch&lt;/tt&gt;. Lets prove a point in this.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;class Dodo {

    public static void main(String...args) {
        int num = doSomething();
        System.out.println(num);
    }

    private static int doSomething() {
        try {
            return (1+2+3);
        }
        catch(Exception e) {
            return -1;
        }
        finally {
            return 0;
        }
    }
}
&lt;/pre&gt;
&lt;br /&gt;
The output will always be &lt;tt&gt;0&lt;/tt&gt;.&lt;br /&gt;
So, basically, I can have a conversation as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Asker:&lt;/span&gt; Dude, what you are gonna do with this finally?&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Me:&lt;/span&gt; Ok dude, since this is the one that will be run no matter how my program logic screws up in the try block, I may write only that part which will not throw any problems like (1+2*3). But I will not. I will not involve my business logic in this love-triangle.&lt;br /&gt;
&lt;span style=&quot;font-family: Courier New, Courier, monospace;&quot;&gt;Asker:&lt;/span&gt; What then?&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Me&lt;/span&gt;: I&#39;d love to clear up things only.&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Asker&lt;/span&gt;: I&#39;m not clear!&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Me&lt;/span&gt;: For example, if I am doing input-output operations, then I will close the connections in finally and nullify the references to help Mr. GC.&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Asker&lt;/span&gt;: Ok&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Courier New&#39;, Courier, monospace;&quot;&gt;Me&lt;/span&gt;: Enjoy!&lt;br /&gt;
&lt;br /&gt;
&lt;hr /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;img alt=&quot;DO NOT TRY THIS AT HOME&quot; border=&quot;0&quot; src=&quot;http://t0.gstatic.com/images?q=tbn:ANd9GcTfn_vc9qH-iT9HhToHixTEJ0yyz2SQztESjeFy10UaO7NOVSBy&quot; /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;pre class=&quot;brush:java&quot;&gt;public class TCFTest {
    static int i;
    public static void main(String[] args) {
        m1();
        System.out.println(i); //What will be the output? ^_^
    }
    static void m1(){
        //the value of static variable &#39;i&#39; will be set
        try{
            i= 1;
            try{
                i= 4;
            }
            finally{
                i= 5;
            }
        }
        catch(Exception e){
            i= 2;
        }
        finally{
            i= 3;
            try{
                i= 6;
            }
            finally{
                i= 7;
            }
        }
    }//m1() end
}
&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://javabambino.blogspot.com/feeds/1639609451193159552/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://javabambino.blogspot.com/2013/10/the-try-catch-finally-love-affair.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/1639609451193159552'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3781349183261963248/posts/default/1639609451193159552'/><link rel='alternate' type='text/html' href='http://javabambino.blogspot.com/2013/10/the-try-catch-finally-love-affair.html' title='The try-catch-finally love affair!'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>