How to count Words, Paragraphs, Sentences, Characters, White Spaces in a Text File using Java

Introduction Here I will create a sample program to count words, paragraphs, sentences, characters, white spaces in a text file using Java programming language. In this tutorial I will read a simple text file and count number of words, paragraphs, characters etc.

How To Group Objects By Their Property Using Java 8 Stream

Introduction Here you 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 or later version of Java directly allows you to do GROUP BY in Java by using Collectors.groupingBy() method….

How To Verify A String Is Palindrome In Java

Introduction In this tutorial I will check how to verify a string is palindrome or not using Java programming language. A palindrome is a technique, where a word, phrase, or sequence that reads the same backwards as forwards. For example, ehcache, madam, etc are examples of palindrome. Prerequisites Java Check Palindrome I will use here few techniques to present how…

Find out how many times each character is showing up in a string

Introduction Here we will create a simple example to count each character to find out how many times each character repeats in the string. A string can have unique characters as well repeated characters. A string can also have only unique characters in it. Let’s say, you are given a string input, for example, “Hello World!”. Your output should be,…

How to find Prime Numbers using Java Programming Language

Introduction In this example we will see how to find prime numbers using Java programming language between 1 and 1000. We will also use Java 8 IntStream API to find prime numbers. We will actually compute prime number between given ranges, let’s say from i to n. So you can replace the starting (i) and ending (n) ranges. A number…

Find Start And End Index Of A Given Element In A Sorted Array

Introduction Here I will find start and end index of a given element in a sorted array using Java programming language. So it means in the sorted array I have to find the start and end indices of a particular element. The array may also contain duplicate elements. Let’s say a sorted array is given with the elements in it…

Segregate Positive followed by Negative Elements in an Array

Introduction This example will show you how to segregate positive followed by negative elements in an Array using Java programming language. The elements are integer values here. The function will put all positive numbers first, and then negative numbers and finally count the number of moves required to arrange all elements in the desired order.

Read last n lines from a file using Java

Introduction Here I will show you how to read last n lines from a file using Java programming language. I am using text file for this example. Last “n” lines means, last 10 lines or 20 lines or 30 lines, etc. So “n” should be replaced by an appropriate non-zero positive integer value. Obviously the tail of the file is…

Minimum Moves to Equalize Array Elements

Introduction Here I will show you how to find out minimum moves to equalize array elements. I will check the equality of elements from two arrays. As a pre-condition both arrays must have equal number of elements, i.e., in other words both arrays must be of same length. I will read each element from both arrays one by one at…

Remove Duplicates from String using Java

Introduction Removes duplicates from String will remove duplicate characters (if any) from String using Java programming language. For example, let’s say we have a string roytuts and we want to remove duplicates from this string. So in this string the duplicate character is t. Therefore after removing duplicate characters we will get the final string roytus.