In this problem, we are given an array of integers and an integer sum and we have to print all pairs of integers whose sum is equal to the sum value.Let’s take an example to understand the problem :Input − array = {1, 6, -2, 3} sum = 4Output − (1, 3) , (6, -2)Here, we need pairs with the given sum value.A simple solution to the problem will be checking pairs of elements that generate the sum. This can be done by traversing array and find the number in array that sums up to sum value.ExampleThis program will illustrate ... Read More
The "this" and "super" references within a lambda expression are the same as in the enclosing context. Since the lambda expression doesn't define a new scope, "this" keyword within a lambda expression signifies "this" parameter of a method where the lambda expression is residing.In the below example, this.toString() calls the toString() method of the LambdaTest object but not the toString() method of the Operate instance.Example@FunctionalInterface interface Operate { int func(int num1, int num2); public String toString(); } public class LambdaTest { public static void main(String[] args) { LambdaTest test = new LambdaTest(); test.getResult(); } ... Read More
IntConsumer interface is a functional interface from java.util.function package in Java 8. This interface accepts a single int-valued argument as input but doesn't produce any output. Since it is a functional interface, it can be used as an assignment target for a lambda expression or method reference. It contains one abstract method: accept() and one default method: andThen().Syntax@FunctionalInterface public interface IntConsumer { void accept(int value); }Example of Lambda Expressionimport java.util.function.IntConsumer; public class IntConsumerTest1 { public static void main(String[] args) { IntConsumer displayNextInt = i -> System.out.println("Next Int Value: " + (i+1)); // lambda IntConsumer displaySquare = ... Read More
In this problem we are given a directed graph and we have to print all paths from the source to the destination of the graph using Breadth first Search (BFS).Directed graph is a graph in with edges that are directed from vertex a to b.Let’s take an example to understand the problem -Source = K destination = POutputK -> T -> Y -> A -> P K -> T -> Y -> P K -> A -> PHere, we have found paths from K to P. We have traversed paths and printed all paths from K that direct us to P.To ... Read More
In this problem, we are given a string of size n and we have to print all permutations of the string. But this time we have to print this permutation using ArrayList.Let’s take an example to understand the problem -Input − string = ‘XYZ’Output − XYZ, XZY, YXZ, YZX, ZXY, ZYXTo solve this problem, we will be generating all permutations of the character of the string. We will use a recursive function and will return arrayList.ExampleThe following is ArrayList implementation of the algorithm − Live Demoimport java.util.ArrayList; public class Main{ static void printArrayList(ArrayList combo) { combo.remove(""); ... Read More
In this problem, we are given a string of n characters and we have to print all permutations of characters of the string. Repeating of characters of the string is allowed. The printing of permutation should be done in alphabetical order (lexicographically sorted order).Let’s take an example to understand the topic better :Input − XYOutput − XX, XY, YX, YYTo solve this problem, we need to use fix and recur logic. Here, we will fix one element at first index of the array and then recursively call for the next elements in the sequence.Let’s see an implementation example which will ... Read More
Now, because of heavy or 24 hours uses of computer at corporate or home place, it becomes a complex and delicate machine. This complex and delicate machine sometime goes wrong while updating BIOS, downloading new drivers, or just surfing the Web. To resolve these issues user does not have to buy an expensive device or replace it with a new one.Microsoft provides a very useful Windows functions called Safe Mode functions. It’s always good to start windows in a safe mode to troubleshoot any kind of issues which give bad effects on the system. Although, using this mode only certain ... Read More
Precondition to check if the list passed as parameter is empty or not. Let us see an example −Examplepublic void my_fun(List myList){ if (myList == null){ throw new IllegalArgumentException("List is null"); } if (myList.isEmpty()){ throw new IllegalArgumentException("List is empty"); } my_fun(myList); }A void function named ‘my_fun’ is defined that takes a list of objects as its parameters. If the list is null, it prints the relevant message. If the list has no elements in it, a specific message is displayed. The function is called by passing the list as ... Read More
Example Live Demopublic class Main{ static volatile boolean exit = false; public static void main(String[] args){ System.out.println("Starting the main thread"); new Thread(){ public void run(){ System.out.println("Starting the inner thread"); while (!exit){ } System.out.println("Exiting the inner thread"); } }.start(); try{ Thread.sleep(100); } catch (InterruptedException e){ ... Read More
Joiner provides various methods to handle joining operations on string, objects, etc. Let us see an example −Exampleimport com.google.common.base.Joiner; import java.util.*; public class Demo{ public static void main(String[] args){ String[] my_arr = { "hel", null, "lo", "wo", "r", null, "ld" }; System.out.println("The original array is : "+ Arrays.toString(my_arr)); String my_result = Joiner.on('+').skipNulls().join(my_arr); System.out.println("The joined string is : " + my_result); } }OutputThe original array is [hel, null, lo, wo, r, null, ld] The joined string is hel+lo+wo+r+ldA class named Demo contains the main function, which defines ... Read More