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.

Mock Activiti TaskService, TaskQuery, NativeTaskQuery in Junit

Introduction In this tutorial we will see how to mock Activiti TaskService, TaskQuery, NativeTaskQuery in Junit class. Suppose you have Spring service class where you are doing some business flow for your application. So you are using Activiti‘s TaskService to create a list of activiti Tasks using the method createNativeTaskQuery(). You may need to create also TaskQuery from TaskService using…

Convert Milliseconds into Days, Hours, Minutes, Seconds in Java

Introduction This example shows you hot to convert milliseconds into days, hours, minutes, seconds in Java. Sometimes you need to convert the milliseconds into days or hours or minutes or seconds. Typically when you want to check the exact time taken to execute a program then you may need to calculate the time. So in this case you get the…

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…

Mock Drools ObjectFactory, KieSession and StatelessKieSession using Junit

This tutorial example will show you how to mock ObjectFactory<KieSession>, KieSession and KieStatelessSession using Junit. KieSession is the most common way to interact with the engine in Drools. A KieSession allows the application to establish an iterative conversation with the engine, where the state of the session is kept across invocations. The reasoning process may be triggered multiple times for…

Check If A Binary Tree Is Binary Search Tree

Binary Search Tree This tutorial will check if a Binary Tree is Binary Search Tree or not. It is known that a binary search tree (BST) is a node based binary tree data structure which satisfies the following properties: I will implement the algorithm using Java program in O(n) time complexity to check whether the Binary Tree is Binary Search Tree…

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.

Minimum moves to segregate even followed by odd elements in Array

Introduction This tutorial will find out minimum moves to segregate even followed by odd elements in an Array using Java programming language. The function will put all even numbers first, and then odd numbers and finally count the number of moves required to arrange all elements in the desired order. This example also works on duplicate elements but does not…

Return array of products of all other numbers from a given array

Introduction This tutorial will show you how to return array of products of all other numbers from a given array. Let’s say you are given an array of non-zero integers. So you have to return an output array of products of all other numbers except the number itself.