Wednesday, 23 May 2018

How to determine if a string has all unique characters in Java?

This is one of the common string based coding problem from programming job interviews. You need to write a program to determine if a given string has all unique characters or not. For example input= "Java" then your function should return false because all characters are not unique, and if the input is "Python" then your program should return true because all characters in Python are unique. For the purpose of this problem, you can assume the given String only contains ASCII printable characters, though you should always verify that with the interviewer. You can also assume that your solution needs to case-sensitive i.e. "P" and "p" will be considered two different characters and a string containing a letter in both capital and the small case will be considered unique. You are also free to solve the problem in place of using any additional data structure.

Monday, 21 May 2018

How to Implement a Power Function in Java - Coding Problems

Even though the Java library has a power function, Math.pow() to calculate the power of a number in Java, it's a common coding exercise for Java programmer to implement a power function. The java.lang.Math.pow(double a, double b) returns the value of the first number raised to the power of the second number and you need to do the same. In other words, write a function to calculate the power of integer numbers for simplicity.

Friday, 18 May 2018

Java - String charAt Example - How to get first and last characters

The charAt(int index) method of java.lang.String class can be used to retrieve a character from given index. The method returns a character, you can see its return type is char. The index starts from zero and ranges to length() - 1 where length() return the length of String. It's similar to an array where the first element is stored at zeroth index and the last element is stored at length -1 index because String is nothing but backed by a character array. If the value of the index is invalid i.e. it's lower than zero or negative or higher than length - 1 then this method throws an IndexOutOfBoundsException. In this article, I'll show you a couple of example of how to use the charAt() method to retrieve a character from String in Java.

Wednesday, 16 May 2018

What is JDBC?

JDBC stands for Java Database Connectivity, which is a standard Java API for database-independent connectivity between the Java programming language and a wide range of databases.

The JDBC library includes APIs for each of the tasks mentioned below that are commonly associated with database usage.

Monday, 14 May 2018

How to Find K Missing Numbers in Integer Array With Duplicates in Java?

It’s been a long time since I have discussed any coding or algorithm interview questions, so I thought to revisit one of the most popular array based coding problem of finding missing numbers in given array. You might have heard or seen this problem before on your programming job interview but there are a lot of different versions of increasing difficulty levels which interviewer normally use to confuse candidate and further test their ability to adapt to frequent changes. In the past I have demonstrated how to find the missing number in a sorted array as well on the unsorted integer array in Java using BitSet, but, with just one missing number and without any duplicates.

Friday, 11 May 2018

Moving a file from one directory to another using Java

Java provides functions to move files between directories. Two ways to achieve this are described here. The first method utilizes Files package for moving while the other method first copies the file to destination and then deletes the original copy from the source.

Wednesday, 9 May 2018

How to Make Collection like ArrayList Read Only in Java?

Read only Collection in Java


You can create read-only Collection by using Collections.unmodifiableCollection() utility method. it returns a unmodifiable or read-only view of Collection in which you can not perform any operation which will change the collection like add() , remove() and set() either directly or while iterating using Iterator or ListIterator. It will throw UnsupportedOperationException whenever you try to modify the List. One of the common misconception around read only ArrayList is that, you can create read-only ArrayList by using Arrays.asList(String{[]), which is apparently not true as this method only return a fixed size list on which add() and remove() are not allowed by set() method is still allowed which can change the contents of ArrayList. Collections class also provide a different method to make List and Set read-only. In this Java tutorial, we will learn How to make any collection read only and How to create fixed size List as well.

Monday, 7 May 2018

Java Tips: Creating a Monitoring-friendly ExecutorService

In this article we will be extending an ExecutorService implementation with monitoring capabilities. This monitoring capability will help us to measure a number of pool parameters i.e., active threads, work queue size etc. in a live production environment. It will also enable us to measure task execution time, successful tasks count, and failed tasks count.

Sunday, 6 May 2018

Multithreading in Oracle Java

Sophisticated computer users become impatient with the do-one-thing-at-a-time mindset of the average personal computer. Users perceive that their world is full of multiple events all happening at once, and they like to have their computers work the same way.

Unfortunately, writing programs that deal with many things happening at once can be much more difficult than writing in the conventional single-threaded C and C++ style. You can write multithreaded applications in languages such as C and C++, but the level of difficulty goes up by orders of magnitude, and even then there are no assurances that vendors' libraries are thread-safe.

Friday, 4 May 2018

Main Features of the Java Programming Language

The Java TM programming language follows C++ to some degree, which carries the benefit of it being familiar to many programmers. This section describes the essential features of the Java programming language and points out where the language diverges from its ancestors C and C++.