↓ Skip to main content

Java

JUNIT5: Learning how to write the code the right way!

JUNIT5: Learning how to write the code the right way!

·2 mins
JUnit was developed by Kent Beck and Erich Gamma. Its first version was released in 1997. JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage — https://junit.org/junit5/docs/current/user-guide/ It is powerful testing framework, with many modules. These modules are part of three sub-projects
Java Stream: Examples

Java Stream: Examples

·3 mins
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)
Smallest Integer

Smallest Integer

·1 min
Code available here. The ask was to identify the smallest possible integer (> 0) in the array of integers. Example # 1. [-1,2] -> 1 2. [-1,-2] -> 1 3. [1,3] -> 2 I was initially thinking of using sets and then finding out the missing one, but then reaized it is simpler and I do not need sets
Fortune v1.0

Fortune v1.0

·4 mins
In one of recent discussions with Mr. X I was asked about my approach to solving the Linux cookie problem A random quote Repository will be a text file Each quote will be segregated by a '%' Can be multiline. I always believe in incremental steps so here is what I could think of
Nth Prime

Nth Prime

·2 mins
Prime number is an age old problem. I am trying to solve it via streams. class PrimeCalculator { public int nth(int nth) { if (nth <= 0) throw new IllegalArgumentException(); // Will skip the nth - 1 // 2 to n-1 return IntStream.iterate(2, i -> i + 1).filter(PrimeCalculator::isPrime).skip(nth - 1).findFirst().getAsInt(); } public static boolean isPrime(int x) { // till half of x return IntStream.rangeClosed(2, (int) (x/2) ).dropWhile(i -> x % i != 0).findFirst().isEmpty(); } } Explanation # public int nth(int nth) { if (nth <= 0) throw new IllegalArgumentException(); // Will skip the nth - 1 // 2 to n-1 return IntStream.iterate(2, i -> i + 1).filter(PrimeCalculator::isPrime).skip(nth - 1).findFirst().getAsInt(); } The method nth(int) allows you to return the nth prime starting at 2, 3, 5....
JavaStream: Characters

JavaStream: Characters

·1 min
Continuing from the last blog how about checking a pangram using streams. public class PangramChecker { private String allChars = "abcdefghijklmnopqrstuvwxyz"; public boolean isPangram(String input) { if (input.length() <= 0 || input.isBlank()){ return false; } return (allChars.chars().map(x -> (char) x).filter(x -> input.toLowerCase().indexOf(x) >= 0).count() == 26); } } All the for loops have been squeezed into a simple line
Java Streams

Java Streams

·2 mins
Stream is an new abstraction that lets one process data in a declarative way. A stream does not store data and, in that sense, is not a data structure. It also never modifies the underlying data source. The easiest way to learn a new API or a construct is by trying it. So here is an exercise that I tried to learn the Stream API. This is an exercise from exercism.io.