A method reference provides a way in the lambda expression to refer a method without executing it. It requires a target type context that consists of a compatible functional interface.Syntax :: In the below example, we can find out the maximum value of an ArrayList using method reference.Exampleimport java.util.*; class MyClass { private int val; MyClass(int v) { val = v; } int getVal() { return val; } } public class MethodReferenceMaxValueTest { static int compareMaxValue(MyClass a, MyClass b) { return a.getVal() - b.getVal(); } public ... Read More
Suppose we have two strings str1 and str2. And their lengths are same, we have to check whether we can transform str1 into str2 by doing zero or more conversions.In one conversion we can convert all occurrences of one character in str1 to any other lowercase English character. We have to check whether we can transform str1 into str2 or not.So, if the input is like str1 = "aabcc", str2 = "ccdee", then the output will be true, as Convert 'c' to 'e' then 'b' to 'd' then 'a' to 'c'. Here we have to keep in mind that the ... Read More
The method references are introduced in Java 8 similar to lambda expression. It can allow us to reference methods or constructors without executing them. The method references and lambda expressions require a target type that consists of a compatible functional interface. We can also use a method reference with generic classes and generic methods in java.Exampleinterface MyFunc { int func(T[] vals, T v); } class MyArrayOps { static int countMatching(T[] vals, T v) { int count = 0; for(int i=0; i < vals.length; i++) if(vals[i] == v) count++; return count; ... Read More
A lambda expression can't specify type parameters, so it's not generic. However, a functional interface associated with lambda expression is generic. In this case, the target type of lambda expression has determined by the type of argument(s) specified when a functional interface reference is declared.Syntaxinterface SomeFunc { T func(T t); }Exampleinterface MyGeneric { T compute(T t); } public class LambdaGenericFuncInterfaceTest { public static void main(String args[]) { MyGeneric reverse = (str) -> { // Lambda Expression String result = ""; for(int i = str.length()-1; i >= 0; i--) ... Read More
Suppose there are N courses, and these are labelled from 1 to N. We also gave a relation array, where relations[i] = [X, Y], is representing a prerequisite relationship between course X and course Y. So, this means course X has to be studied before course Y.In one semester we can study any number of courses as long as we have studied all the prerequisites for the course we are studying. We have to find the minimum number of semesters needed to study all courses. And if there is no way to study all the courses, then return -1.So, if ... Read More
Suppose we have a non-decreasing array of positive integers called nums and an integer K, we have to find out if this array can be divided into one or more number of disjoint increasing subsequences of length at least K.So, if the input is like nums = [1, 2, 2, 3, 3, 4, 4], K = 3, then the output will be true, as this array can be divided into the two subsequences like [1, 2, 3, 4] and [2, 3, 4] with lengths at least 3 each.To solve this, we will follow these steps −d := a new mapreq ... Read More
Suppose we have a digit, now if we rotate that digit by 180 degrees to form new digits. When 0, 1, 6, 8, 9 are rotated 180 degrees, they become 0, 1, 9, 8, 6 respectively. But when 2, 3, 4, 5 and 7 are rotated 180 degrees, they become invalid.A confusing number is a number that when rotated 180 degrees becomes a new number. So, if we have a positive integer N, we have to find the number of confusing numbers between 1 and N inclusive.So, if the input is like 20, then the output will be 6To solve ... Read More
Suppose we have an integer d between 0 and 9, we also have two positive integers low and high as lower and upper bounds, respectively. We have to find the number of times that d occurs as a digit in all integers between low and high, including the bounds low and high.So, if the input is like d = 1, low = 1, high = 13, then the output will be 6, as digit d=1 occurs 6 times like 1, 10, 11, 12, 13.To solve this, we will follow these steps −Define a function zero(), this will take n, ret ... Read More
In this article, we will learn about the solution to the problem statement given below.Problem statement − We are given a list, we need to swap the last element with the first element.There are 4 approaches to solve the problem as discussed below−Approach 1 − The brute-force approachExample Live Demodef swapLast(List): size = len(List) # Swap operation temp = List[0] List[0] = List[size - 1] List[size - 1] = temp return List # Driver code List = ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l'] print(swapLast(List))Output['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']Approach 2 − The ... Read More
Suppose we have an array A of integers, we have to find the number of non-empty continuous subarrays that satisfy this condition: The leftmost element of the subarray is not larger than other elements in the subarray.So, if the input is like [1, 4, 2, 5, 3], then the output will be 11, as there are 11 valid subarrays, they are like [1], [4], [2], [5], [3], [1, 4], [2, 5], [1, 4, 2], [2, 5, 3], [1, 4, 2, 5], [1, 4, 2, 5, 3].To solve this, we will follow these steps −ret := 0n := size of numsDefine ... Read More