Merge Two String Arrays and Produce Unique Values using Java

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…

Continue

How 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.

Continue

How 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.

Continue

How 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…

Continue

Find 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.

Continue

How 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…

Continue

How 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