<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Java on Samarthya</title><link>https://samarthya.me/categories/java/</link><description>Recent content in Java on Samarthya</description><generator>Hugo -- gohugo.io</generator><language>en</language><copyright>© Samarthya 2018-2026. All rights reserved.</copyright><lastBuildDate>Wed, 13 Jul 2022 12:00:00 +0000</lastBuildDate><atom:link href="https://samarthya.me/categories/java/index.xml" rel="self" type="application/rss+xml"/><item><title>Naming Convention Policy for Kafka topics</title><link>https://samarthya.me/posts/naming-convention-policy-for-kafka-topics/</link><pubDate>Wed, 13 Jul 2022 12:00:00 +0000</pubDate><guid>https://samarthya.me/posts/naming-convention-policy-for-kafka-topics/</guid><description>&lt;p&gt;Creating a &lt;strong&gt;Topic&lt;/strong&gt; in a kafka-cluster is easy and well documented for the &lt;code&gt;kafka-topics.sh&lt;/code&gt; or even the official API documentation.&lt;/p&gt;&#10;&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; bin/kafka-topics.sh --help&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&#10;&lt;p&gt;The complexity arises, when you are trying to enforce a standard way of defining topic naming. There are many ways to identify the &lt;strong&gt;right convention&lt;/strong&gt; based on your need, but to enforce such a topic convention while you are creating one is explained in this 5 steps blog.&lt;/p&gt;</description></item><item><title>JUNIT5: Learning how to write the code the right way!</title><link>https://samarthya.me/posts/junit5-learning-how-to-write-the-code-the-right-way/</link><pubDate>Sat, 05 Jun 2021 11:31:24 +0000</pubDate><guid>https://samarthya.me/posts/junit5-learning-how-to-write-the-code-the-right-way/</guid><description>&lt;p&gt;JUnit was developed by &lt;strong&gt;Kent Beck&lt;/strong&gt; and &lt;strong&gt;Erich Gamma&lt;/strong&gt;. Its first version was released in &lt;strong&gt;1997&lt;/strong&gt;.&lt;/p&gt;&#10;&lt;div class="lead text-neutral-500 dark:text-neutral-400 !mb-9 text-xl"&gt;&#10; &lt;strong&gt;JUnit 5 = &lt;em&gt;JUnit Platform&lt;/em&gt; + &lt;em&gt;JUnit Jupiter&lt;/em&gt; + &lt;em&gt;JUnit Vintage&lt;/em&gt;&lt;/strong&gt;&#10;— &lt;a href="https://junit.org/junit5/docs/current/user-guide/" target="_blank" rel="noreferrer"&gt;https://junit.org/junit5/docs/current/user-guide/&lt;/a&gt;&#10;&lt;/div&gt;&#10;&#10;&lt;p&gt;It is powerful testing framework, with many modules. These modules are part of three sub-projects&lt;/p&gt;</description></item><item><title>Java Stream: Examples</title><link>https://samarthya.me/posts/java-stream-examples/</link><pubDate>Mon, 26 Apr 2021 09:07:19 +0000</pubDate><guid>https://samarthya.me/posts/java-stream-examples/</guid><description>&lt;p&gt;Streams are convenient way to reduce the line of codes, but an understanding them is equally challenging. I write this blog, after realising a desperate attempt at solving a trivial problem just took overwhelming time. (Could be that I am getting old, but never late to learn things again)&lt;/p&gt;</description></item><item><title>Smallest Integer</title><link>https://samarthya.me/posts/smallest-integer/</link><pubDate>Sat, 24 Apr 2021 05:43:46 +0000</pubDate><guid>https://samarthya.me/posts/smallest-integer/</guid><description>&lt;p&gt;Code available &lt;a href="https://github.com/samarthya/smallestinteger" target="_blank" rel="noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;The ask was to identify the smallest possible integer (&amp;gt; 0) in the array of integers.&lt;/p&gt;&#10;&#10;&lt;h2 class="relative group"&gt;Example&#10; &lt;div id="example" class="anchor"&gt;&lt;/div&gt;&#10; &#10; &lt;span&#10; class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;&#10; &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#example" aria-label="Anchor"&gt;#&lt;/a&gt;&#10; &lt;/span&gt;&#10; &#10;&lt;/h2&gt;&#10;&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;1. [-1,2] -&amp;gt; 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;2. [-1,-2] -&amp;gt; 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;3. [1,3] -&amp;gt; 2&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&#10;&lt;p&gt;I was initially thinking of using &lt;code&gt;sets&lt;/code&gt; and then finding out the missing one, but then reaized it is simpler and I do not need sets&lt;/p&gt;</description></item><item><title>Fortune v1.0</title><link>https://samarthya.me/posts/fortune-v1-0/</link><pubDate>Wed, 16 Dec 2020 10:50:24 +0000</pubDate><guid>https://samarthya.me/posts/fortune-v1-0/</guid><description>&lt;p&gt;In one of recent discussions &lt;code&gt;with Mr. X&lt;/code&gt; I was asked about my approach to solving the Linux cookie problem&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;A random quote&lt;/li&gt;&#10;&lt;li&gt;Repository will be a text file&lt;/li&gt;&#10;&lt;li&gt;Each quote will be segregated by a &lt;code&gt;'%'&lt;/code&gt;&lt;/li&gt;&#10;&lt;li&gt;Can be multiline.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;I always believe in incremental steps so here is what I could think of&lt;/p&gt;</description></item><item><title>Nth Prime</title><link>https://samarthya.me/posts/nth-prime/</link><pubDate>Tue, 15 Dec 2020 08:21:32 +0000</pubDate><guid>https://samarthya.me/posts/nth-prime/</guid><description>&lt;p&gt;Prime number is an age old problem. I am trying to solve it via streams.&lt;/p&gt;&#10;&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;class PrimeCalculator {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; public int nth(int nth) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if (nth &amp;lt;= 0)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; throw new IllegalArgumentException();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; // Will skip the nth - 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; // 2 to n-1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return IntStream.iterate(2, i -&amp;gt; i + 1).filter(PrimeCalculator::isPrime).skip(nth - 1).findFirst().getAsInt();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; public static boolean isPrime(int x) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; // till half of x&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return IntStream.rangeClosed(2, (int) (x/2) ).dropWhile(i -&amp;gt; x % i != 0).findFirst().isEmpty();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&#10;&#10;&lt;h2 class="relative group"&gt;Explanation&#10; &lt;div id="explanation" class="anchor"&gt;&lt;/div&gt;&#10; &#10; &lt;span&#10; class="absolute top-0 w-6 transition-opacity opacity-0 -start-6 not-prose group-hover:opacity-100 select-none"&gt;&#10; &lt;a class="text-primary-300 dark:text-neutral-700 !no-underline" href="#explanation" aria-label="Anchor"&gt;#&lt;/a&gt;&#10; &lt;/span&gt;&#10; &#10;&lt;/h2&gt;&#10;&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;public int nth(int nth) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if (nth &amp;lt;= 0)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; throw new IllegalArgumentException();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; // Will skip the nth - 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; // 2 to n-1&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return IntStream.iterate(2, i -&amp;gt; i + 1).filter(PrimeCalculator::isPrime).skip(nth - 1).findFirst().getAsInt();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&#10;&lt;p&gt;The method &lt;code&gt;nth(int)&lt;/code&gt; allows you to return the nth prime starting at &lt;code&gt;2, 3, 5....&lt;/code&gt;&lt;/p&gt;</description></item><item><title>JavaStream: Characters</title><link>https://samarthya.me/posts/javastream-characters/</link><pubDate>Fri, 13 Nov 2020 10:20:02 +0000</pubDate><guid>https://samarthya.me/posts/javastream-characters/</guid><description>&lt;p&gt;Continuing from the last blog how about checking a pangram using streams.&lt;/p&gt;&#10;&lt;div class="highlight-wrapper"&gt;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;public class PangramChecker {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; private String allChars = &amp;#34;abcdefghijklmnopqrstuvwxyz&amp;#34;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; public boolean isPangram(String input) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; if (input.length() &amp;lt;= 0 || input.isBlank()){&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return false;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; return (allChars.chars().map(x -&amp;gt; (char) x).filter(x -&amp;gt; input.toLowerCase().indexOf(x) &amp;gt;= 0).count() == 26);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&#10;&lt;p&gt;All the for loops have been squeezed into a simple line&lt;/p&gt;</description></item><item><title>Java Streams</title><link>https://samarthya.me/posts/java-streams/</link><pubDate>Thu, 12 Nov 2020 11:47:49 +0000</pubDate><guid>https://samarthya.me/posts/java-streams/</guid><description>&lt;p&gt;&lt;em&gt;Stream&lt;/em&gt; is an new abstraction that lets one process data in a declarative way.&lt;/p&gt;&#10;&lt;div class="lead text-neutral-500 dark:text-neutral-400 !mb-9 text-xl"&gt;&#10; &lt;strong&gt;A stream does not store data and, in that sense, is not a data structure. It also never modifies the underlying data source.&lt;/strong&gt;&#10;&lt;/div&gt;&#10;&#10;&lt;p&gt;The easiest way to learn a new API or a construct is by trying it. So here is an &lt;a href="https://github.com/samarthya/javaex/tree/local/grains" target="_blank" rel="noreferrer"&gt;exercise&lt;/a&gt; that I tried to learn the Stream API. This is an exercise from &lt;a href="https://exercism.io/my/solutions/428fcd2c497c4098b5bd3121a1d66110" target="_blank" rel="noreferrer"&gt;exercism.io&lt;/a&gt;.&lt;/p&gt;</description></item></channel></rss>