Introduction In this tutorial we will show you how to merge string values from two arrays and produce the final array with unique string values. We will use Set data structure to produce the unique results in final array. When passed two arrays of strings, it will return an array containing the string values that appear in either or both arrays. The returned array should have no duplicates. For example, calling mergeArrays(new String[]{‘Ava’, ‘Emma’, ‘Olivia’}, new String[]{‘Olivia’, ‘Sophia’, ‘Emma’}) should return an array containing Ava, Emma, Olivia, and Sophia in…
ContinueTag: Java Coding
Find closest number from array to the given number in Java
In this tutorial we will find closest number from array to the given number. Given an array of sorted integers, find the closest value to the given number. The array may contain duplicate values and negative numbers. Examples Array : 2,5,6,7,8,8,9Target number : 5Output : 5
ContinueHow to check or make a number palindrome in Java
Here we will create an example on how to check a given number is palindrome or not. We will also make the given number palindrome if it is not palindrome. In our other tutorial we have seen how to check a string is palindrome or not. A string or number is palindrome if it is equal to its reversed counterpart. For example, madam, 11, 22, etc.
ContinueHow to find longest substring without repeating characters using Java
Introduction In this example we will see how to find longest substring from a given string without repeating characters using Java programming language. A given string when broken into substring will have different lengths and we will find the string which has longest length among them. A string may consist of contiguous characters or several words separated by spaces.
ContinueHow to shuffle an ArrayList using Java
Introduction In this example we will see how to shuffle an ArrayList using Java programming language. We will use shuffle() method from Collections class to shuffle the ArrayList. We will also create our own method to shuffle the ArrayList. Shuffle means rearrange the elements of the ArrayList by sliding them over each other quickly. So we will basically swap or randomize the positions of the elements in the ArrayList. To calculate the random positions of the ArrayList, we will use random() method on the maximum size of the existing ArrayList.
ContinueHow to reverse a string without affecting special characters using Java
Introduction In this tutorial we will see how to reverse a string without affecting special characters using Java programming language. We will also see in this example how to check whether a character is alphabet or digit or special character. Your string may contain special characters such as $, #, @ etc. and you don’t want to change their positions in the string, so this example shows hot to reverse the string without changing their position. The string has to reversed and the special character must stay unchanged in the…
ContinueFind largest product of two elements in an array using Java
Introduction In this example we will see how to find largest product of two elements in an array using Java language. In other hand you can also find those two elements for which the largest product is computed among the elements of an array. The elements are assumed to be integer in an array. The element may be 0 or negative integer. We will write a Java program to find out the largest product of two elements in an array and without sorting the array elements.
ContinueHow to count words, paragraphs, sentences, characters, white spaces in a text file using Java
Introduction Here we will create a sample program to count words, paragraphs, sentences, characters, white spaces in a text file using Java programming language. In this tutorial we will read a simple text file and count number of words, paragraphs, characters etc.
ContinueHow to group objects by their property using Java 8 stream
Introduction Here we will see an example on how to group objects by their property using Java 8 stream API. GROUP BY is a very useful aggregate operation in SQL, which allows us to group records on certain criteria and Java 8 directly allows us to do GROUP BY in Java by using Collectors.groupingBy() method. We have various overloaded version of groupingBy() method which allow us to perform grouping objects concurrently by using concurrent Collectors. Let’s say you have Book object with attributes or properties as title and author and…
ContinueHow to verify a string is palindrome in Java
Introduction In this tutorial we will check how to verify a string is palindrome or not using Java programming language. A palindrome is is a technique, where a word, phrase, or sequence that reads the same backwards as forwards. For example, ehcache, madam etc. Prerequisites Java Check Palindrome using Java We will create here several techniques to present how to check a string is palindrome or not. 1st Approach In first approach we will use while loop to extract each char from the given string. 2nd Approach In second approach…
Continue