HTML

How to use Callable Statement in Java to call Stored Procedure? JDBC Example

The CallableStatement of JDBC API is used to call a stored procedure from Java Program. Calling a stored procedure follows the same pattern as creating PreparedStatment and than executing it. You first need to create a database connection by supplying all the relevant details e.g. database URL, which comprise JDBC protocol and hostname, username, and password. Make sure your JDBC URL is acceptable by JDBC driver you are using to connect to the database. Every database vendor uses different JDBC URL and they have different driver JAR which must be in your classpath before you can run the stored procedure from Java Program.

How to remove duplicates from Stream in Java 8 - Stream.distinct() Example

You can use the Stream.distinct() method to remove duplicates from a Stream in Java 8 and beyond. The distinct() method behaves like distinct clause of SQL which removes duplicate rows from the result set. The distinct() is also an intermediate method which means it will return a new Stream without duplicates, which can be used for further processing. Like other methods of Stream class e.g. map() or filter(), distinct() is also lazy and it will not remove duplicate elements until you call a terminal method on Stream.  The actual processing of Stream pipeline starts only after calling terminal methods like collect() or forEach().

How to convert int[] to ArrayList in Java 8? Example Tutorial

I once asked this question to one of the Java developers during Interview and like many other, he answered that Arrays.asList() can convert an array of primitive integer values to ArrayList<Integer> in Java, which was actually wrong. Even though, Arrays.asList() is the go-to method to convert an array to ArrayList in Java when it comes to converting a primitive array to ArrayList, this method is not useful. The Arrays.asList() method does not deal with boxing and it will just create a List<int[]> which is not what you want. In fact, there was no shortcut to convert an int[] to ArrayList<Integer> or long[] to ArrayList<Long> till Java 8.

How to copy elements of one array to another array in Java - Arrays.copyOf and Arrays.copyOfRange Example

There are multiple ways to copy elements from one array in Java e.g. you can manually copy elements by using a loop, create a clone of the array, use Arrays.copyOf() method or System.arrayCopy() to start copying elements from one array to another in Java. Even though both allow you to copy elements from source to destination array, the Arrays.copyOf() is much easier to use as it takes the just original array and the length of the new array. But, this means you cannot copy subarray using this method because you are not specifying to and from an index, but don't worry there is another method in the java.util.Arrays class to copy elements from one index to other in Java, the Arrays.copyOfRange() method. Both methods are overloaded to copy different types of array.

How to Rotate an Array to Left/Right by a Given Number in Java - Coding Problem

Hello guys, welcome to this post with another array based coding problem. In the last article, we've learned about finding missing numbers in the array with duplicates and today we'll solve the problem of rotating an array by left or right by a given number. For example, suppose an integer array {1, 2, 3} is given and it was asked to rate this array to the left by 3 then the result array will look like {2, 3, 1} because it was rotated twice on left. Similarly, if you are asked to rotate the same array twice by right then you'll get {1, 2, 3}, the same array is back. This is an interesting problem and it's quite easy to solve but I have seen many programmers struggle with this one as well. So, a little bit of practice and learning will not hurt.

Java 8 Optional Example - isPresent(), OrElse() and get()

The Optional class in Java is one of many goodies we have got from the Java 8 release. If you use correctly, Optional can result in clean code and can also help you to avoid NullPointerException which has bothered Java developer from its inception. Even though many of us have used null to indicate the absence of something, the big problem is that if you call a method or access a field on the null object (except static fields), you will get a NullPointerException and your whole program may crash. The bigger problem is to find the faulty code or root cause because NullPointerException only indicates the line when you try to access field or variable from a null object but how does that null is get created on the code is unknown.

How to sort an Array in descending order in Java - Example

Sorting an array is one of the common tasks in Programming and you have many algorithms to sort an array like QuickSort, MergeSort which provides O(NLogN) time performance and Bucket Sort, Counting Sort and Radix Sort algorithms which can even sort some array in O(N) time. But, you hardly need to code these algorithms by hand when it comes to writing real code. The Programming language you will use already have tried and tested implementation for those algorithms and that's what you will learn in this article. In Java Programming language, it's easy to sort an array, you just need to call the Arrays.sort() method with a Comparator which can sort the array in the order you want but it highly depends upon which type of object is stored in the array.

Java 8 Map Function Examples

The map is a well known functional programming concept which is incorporated into Java 8. Map is a function defined in java.util.stream.Streams class, which is used to transform each element of the stream by applying a function to each element. Because of this property, you can use map() in Java 8 to transform a Collection, List, Set or Map. For example, if you have a list of String and you want to convert all of them into upper case, how will you do this? Prior to Java 8, there is no function to do this. You had to iterate through List using a for loop or foreach loop and transform each element. In Java 8, you get the stream, which allows you to apply many functional programming operators like the map, reduce, and filter.

How Binary Search Algorithm Works - Java Example Without Recursion

The binary search algorithm is one of the fundamental Computer Science Algorithms and used to search an element in a sorted input set. It's much faster than the linear search which scans each and every element and improves performance from O(n) to O(logN) for searching an element in the array. In order to perform the binary search, you need a sorted array, so you can either ask the user to enter array in sorted order or you should sort the array before performing the binary search. It's also one of the popular algorithms on Programming Job interviews. Interviewer often asks candidates to implement binary search algorithm by hand in their favorite programming languages like Java, C++, Python. or JavaScript.

Insertion Sort Algorithm in Java with Example

Insertion sort is another simple sorting algorithm like Bubble Sort. You may not have realized but you must have used Insertion sort in a lot of places in your life. One of the best examples of Insertion sort in real-world is, how you sort your hand in playing cards. You pick one card from the deck, you assume it's sorted, and then we insert subsequent card in their proper position. For example, if your first card is Jack, and the next card is Queen then you put the queen after Jack. Now if the next card is King, we put it after the queen, and if we get 9, we put it before jack. So if you look closely, Insertion sort is a perfect sorting algorithm to insert a new value into an already sorted array. That's why the best-case complexity of insertion sort is O(n), in which case you can just insert a new number in the already sorted list of integers.

Java Program to Reverse an Array in place? Fastest Example

It's relatively 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 and other beginner Programmers. But, don't worry, I'll tell you how you can solve this problem without losing your cool. 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 or in-place as described in many Algorithms books and courses, and on Coding interviews.

QuickSort Algorithm Example in Java using Recursion - Sorting Algorithm Implementation

The Quicksort algorithm is one of the very popular sorting algorithms in programming, often used to sort a large array of numbers. Though there is numerous algorithm available to sort a list of objects, including integer, string and floating point number, quicksort is best for general purpose. It's a divide and conquers algorithm, where we divide the given array with respect to a particular element, known as 'pivot' such that the lower partition of the array are less than the pivot and upper partition elements of the array are higher than the pivot. The Quicksort is also one of the best examples of recursion, a key programming technique to solve Algorithmic problems. This algorithm is naturally recursive because it sorts the large list by dividing into smaller sub-list and then applying the same algorithm on those.

How to convert ArrayList to HashMap or LinkedHashMap in Java 8 - Example Tutorial

One of the common task in Java is to convert a List of object e.g. List<T> into a Map e.g. Map<K, V>, where K is some property of the object and V is the actual object. For example, suppose you have a List<Order> and you want to convert it into a Map e.g. Map<OrderId, Order>, how do you that? Well, the simplest way to achieve this is iterating over List and add each element to the Map by extracting keys and using the actual element as an object. This is exactly many of us do it in pre-Java 8 world but JDK 8 has made it even simpler. In Java 8, you can get the stream from List and then collect all elements into a Map by using a Collector. The collect() method of Stream class and java.util.stream.Collectors class gives you ample choices to decide which property goes into the key and which object goes into the value.

How to Remove an Element from Array in Java with Example

There is no direct way to remove elements from an Array in Java. Though Array in Java objects, it doesn't provide any methods to add(), remove() or search an element in Array. This is the reason Collection classes like ArrayList and HashSet are very popular. Thanks to Apache Commons Utils, You can use there ArrayUtils class to remove an element from array more easily than by doing it yourself. One thing to remember is that Arrays are fixed size in Java, once you create an array you can not change their size, which means removing or deleting an item doesn't reduce the size of the array. This is, in fact, the main difference between Array and ArrayList in Java.

7 Examples of HttpURLConnection in Java - Sending GET and POST Request

The HttpURLConnection is an important class in java.net package which allows you to send an HTTP request from Java program. By using this you can send any kind of HTTP request e.g. GET, POST, PUT, DELETE, HEAD etc. It also provides several methods to configure your HTTP requests e.g. it allows you to add headers, add request parameters, attaching cookie, configuring timeout, handling redirects and so on. A good knowledge of this class is important for Java developers working on both core Java and Java web application.

21 String Programming and Coding Interview Questions With Solutions

In this article, I am going to share 21 of the most common String based Programming and Coding interview questions from Java developer interviews. These questions require you to write code to solve the problem and they are different from traditional Java String questions like how substring method works in Java or when to use the intern() method of String in Java? Since coding and problem solving is an important part of any programming job interview, it's imperative that you know how to solve them in time and on pressure situation, which comes only after doing practice with a right set of questions. Since these questions are already tried and tested and appeared in many interviews, they will provide you the experience you need to crack your coding interview.

10 Books for Coders of All levels - Must Read Best of Lot

Coding is one of the most important skills for programmers and if you want to survive as a programmer you must improve your coding skill. Gone are the days, when you can get a software development job without having you to write a single line of code and then somehow survives in the job by doing support, coordination, and other non-coding activities. Companies, both big and small are increasingly testing coding skills of candidates before hiring. It's very common for programmers to pass a coding test before a telephonic round of interview. Some of the companies even hire websites like HackerRank and others to check the coding skill of potential candidates, many others give candidates take home coding tests. In short, companies have a lot of focus on the coding skill of potential hires.