Wednesday, 31 January 2018

How to use Queue in Java - Example

The JDK provides implementation of several data structure in Java collection framework e.g. ArrayList is a dynamic array, LinkedList represents a linked list, HashMap represents hash table data structure, and Queue interface represent queue data structure. The Queue data structure allows you to process elements in the first in first out order and that's why it is also known as FIFO data structure. In order to provide first in first out functionality, it allows you to insert data at one end and delete data from other ends, also known as consuming data. If you need to process objects in the FIFO order you can use a Queue to store them.

Monday, 29 January 2018

How to Create Random Alphabetic or AlphaNumeric String of given length in Java - Example

Suppose, you want to generate an alphabetic or alpha-numeric string of given length in Java? How do you do it? Well, if you are like me, you probably search a library e.g. Apache commons-lang for something which can do this task. It's a good thing. There is no point on re-inventing a wheel if a tried and tested solution already exists. In fact, Effective Java, the most respected book in Java world also suggest to know and use your library. In fact, Apache commons-lang does have a class called RandomStringUtils, which allows you to generate Random alphanumeric String in just one method call as shown below:

Sunday, 28 January 2018

How to change date format of String in Java 8

One of the common programming task in Java is to change the date format of a given Date or String. For example, you have something like "2017-01-18 20:10:00" and you want to convert it that date into "2017-01-18", or you want to convert from dd-MM-YY to MM-dd-YY or to any other format of your choice and need, but a valid date format as per Java specification. How will you do that? Well, it's not that difficult. It's just a two-step process. In the first step, you need to parse String to create an equivalent date using current format and then once you got the date, you need to again convert it back to String using the new format. The same process is repeated in both Java 8 and before, only corresponding API and classes changes.

Friday, 26 January 2018

How to lock a File before writing in Java?

A file is one of the oldest ways to store data and share data but if you are working in a shared file i.e a file which can be read or write by multiple readers and writers, you need to make sure that the file is locked before you try to write on it. This is needed to ensure that someone doesn't overwrite the data you are writing. Fortunately, Java provides a mechanism to lock a file before writing using the FileLock interface. You can get the handle of FileLock by using FileChannel for writing to a file. The FileChannel class is generally used to write faster in the large file and one of the common way to write binary data in Java.

Wednesday, 24 January 2018

3 Ways to Generate Random Integers on a Range in Java

Manytimes you need to generate random numbers, particular integers in a range but unfortunately, JDK doesn't provide a simple method like nextIntegerBetween(int minInclusive, int maxExclusive), because of that many Java progarmmers, particularly beginners struggle to generate random numbers between a range e.g. random integers between 1 to 6 if you are creating a game of dice, or random number between 1 to 52 if you are creating a game of playing cards and you need to choose a random card, or most commonly random numbers between 1 to 10 and 1 to 100. Then, the question comes, how to solve this problem? How to generate random int values between a range? Well, you need to do a little bit of work.

Monday, 22 January 2018

Eclipse IDE - How to Remote Debug a Java Application Running on Linux

The remote debugging of Java program is an ultimate tool in the arsenal of a Java developer, which is often become the last and only tool to investigate a bug on a Java application running on remote host e.g. on Linux server or Windows server. Almost all major Java IDE provides remote debugging e.g. NetBeans, Eclipse, and IntelliJ IDEA, but I mostly use Eclipse for Java coding and so it's my preferred tool to remote debug a Java program. In order to setup remote debugging in Eclipse, you need to do a couple of tasks e.g. you need to start your JVM with debugging parameters or arguments and then you need to create a "remote debug configuration" in Eclipse IDE itself.

Friday, 19 January 2018

10 Important points about String in Java

someone might question that why do we need another article about string in Java, haven't we had enough about the string is already written? Yes, I agree there are a lot of articles related to Java string covering topics like how to use Java String, an example of Java string and almost everything you can do with string in Java. But, I wanted to document my experience of using String in Java and some nasty and little-known things I discovered about the Java String class, which may not be obvious to many Java developers. A string is available in almost every programming language and its probably the most used data structure or a data type, hence a good knowledge of String is very important for every Java developers.

Wednesday, 17 January 2018

JDBC - How to get Row and Column Count from ResultSet in Java

One of the common problem in JDBC is that there is no way to get the total number of records returned by an SQL query. When you execute a Statement, PreparedStatement, or CallableStatement using execute()or executeQuery() they return ResultSet and it doesn't have any method to return the total number of records it is holding. The only way to find the total number of records is keeping the count while you are iterating over ResultSet while fetching the result. This way, you can print the total number of rows returned the SQL query but only after you have processed all records and not before, which may not be the right way and incur significant performance cost if the query returns a large number of rows.

Monday, 15 January 2018

Java Reflection, but much faster

What is the fastest way to read a getter from a Java class without knowing the class at compilation time? Java frameworks often do this. A lot. And it can directly influence their performance. So let’s benchmark different approaches, such as reflection, method handles and code generation.

Friday, 12 January 2018

Java 9 Module Services

Wiring and Finding

Java has a ServiceLoader class for long time. It was introduced in 1.6 but a similar technology was in use since around Java 1.2. Some software components used it, but the use was not widespread. It can be used to modularize the application (even more) and to provide a mean to extend an application using some kind of plug-ins that the application does not depend on compile time. Also, the configuration of these services is very simple: just put it on the class/module path. We will see the details.

Wednesday, 10 January 2018

Java Program to reverse an array in place? Fastest Example

It's easy to reverse an array if you have the luxury to use another array, but how would you reverse an array if a temporary buffer is not allowed? This is one of the testing array interview questions, which often proved tricky for Java programmers. Well, you can also reverse an array in place without using an additional buffer. If you know how to access array elements and how to loop over an array in Java using traditional for loop, you can easily solve this problem without using additional space. All you need to do is a loop over the array from start to the middle element and swap the first element to the last, second element to the second last etc. Once you reach the middle element, your array is already sorted and that too without using any additional space. You can even use this algorithm to reverse a String in Java as well. After all, a String is backed by character array.

Monday, 8 January 2018

How to implement Linear Search in Java? Example

Linear search is nothing but iterating over the array and comparing each element with target element to see if they are equal since we search the array sequential from start to end, this is also known as sequential search or linear search. It is very slow as compared to binary search because you have to compare each element with every other element and definitely not suitable for a large array. It's practically useful only in case of the small array up to 10 to 15 numbers. In the worst case, you need to check all elements to confirm if target element exists in an array or not.

Wednesday, 3 January 2018

Simple String Representation of Java Decimal Numbers without Scientific Notation

The primary types/objects used for decimal numbers in Java are float/Float, double/Double, and BigDecimal. Each of these has cases in which its “default” string representation is “computerized scientific notation.” This post demonstrates some simple approaches to provide a string representation of the decimal number in these cases without scientific notation.

Monday, 1 January 2018

Choosing Java Cryptographic Algorithms Part 1 – Hashing


I did all of the work for this post using the following major technologies. You may be able to do the same thing with different technologies or versions, but no guarantees.

◉ Java 1.8.0_152_x64
◉ NetBeans 8.2 (Build 201609300101)
◉ Maven 3.0.5 (Bundled with NetBeans)