Rules for the Body of Lambda Expression in Java

raja
Updated on 11-Jul-2020 12:48:49

1K+ Views

A lambda expression is an anonymous function (nameless function) that has passed as an argument to another function. We need to follow some rules while using the body of a lambda expression.Rules for the body of a lambda expressionThe body of the lambda expression can be either a single expression or more statements.If we are using a single expression as the body of a lambda expression, then no need to enclose the body with curly braces ({}).If we are using one or more statements as the body of a lambda expression, then enclosing them within curly braces({}) can be mandatory.Syntax(parameters) OR () -> {body with statements separated by;} OR Single StatementExampleinterface Message {   ... Read More

Palindrome Removal in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:46:04

340 Views

Suppose we have an integer array called arr, now in one move we can select a palindromic subarray from index i to j where i

Divide Chocolate in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:43:46

384 Views

Suppose we have one chocolate bar that consists of some chunks. In each chunk it has its own sweetness given by a list called sweetness. If we want to share the chocolate among K friends so we start cutting the chocolate bar into K+1 piece using K cuts, now each piece consists of some consecutive chunks. If we take out the piece with the minimum total sweetness and give the other pieces to our friends. We have to find the maximum total sweetness of the piece we can get by cutting the chocolate bar optimally.So, if the input is like ... Read More

Why We Use Lambda Expressions in Java

raja
Updated on 11-Jul-2020 12:43:42

4K+ Views

A lambda expression can implement a functional interface by defining an anonymous function that can be passed as an argument to some method.Enables functional programming: All new JVM based languages take advantage of the functional paradigm in their applications, but programmers forced to work with Object-Oriented Programming (OOPS) till lambda expressions came. Hence lambda expressions enable us to write functional code.Readable and concise code: People have started using lambda expressions and reported that it can help to remove a huge number of lines from their code.Easy-to-Use APIs and Libraries: An API designed using lambda expressions can be easier to use and support other API.Enables support for ... Read More

Sort Collection Using Stream API with Lambdas in Java

raja
Updated on 11-Jul-2020 12:43:12

528 Views

A Stream API is a powerful way to achieve functional programming in Java. It usually works in conjunction with a lambda expression and provides an efficient way to perform data manipulation operations like sort, filter, map, reduce and etc.In the below example, we can sort a collection using Stream API. It provides sorting logic by using the sorted() method of the Comparator interface. If we have two Comparator interface instances and need to do sorting by composite condition (by the first comparator and then by the second comparator), we can use both comparators by invoking the thenComparing() method on the first instance and passing in the second instance.Exampleimport java.util.*; import java.util.stream.*; ... Read More

Valid Palindrome III in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:40:52

477 Views

Suppose we have a string s and another number k; we have to check whether the given string is a K-Palindrome or not.A string is said to be K-Palindrome if it can be transformed into a palindrome by removing at most k characters from it.So, if the input is like s = "abcdeca", k = 2, then the output will be true as by removing 'b' and 'e' characters, it will be palindrome.To solve this, we will follow these steps −Define a function lcs(), this will take s, t, n := size of sadd one blank space before sadd one ... Read More

Minimum Time to Build Blocks in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:38:21

194 Views

Suppose we have a list of blocks, if we have blocks[i] = t, this means that the i-th block needs t units of time to be built. A block can only be built by exactly one worker. Single worker can either split into two workers or build a block then go home. These two decisions take some time. The time cost of spliting one worker into two workers is given as a number called split.So, if the input is like blocks = [1, 2], and split = 5, then the output will be 7 as we can split the worker ... Read More

Importance of Predicate Interface in Lambda Expression in Java

raja
Updated on 11-Jul-2020 12:38:14

2K+ Views

Predicate is a generic functional interface that represents a single argument function that returns a boolean value (true or false). This interface available in java.util.function package and contains a test(T t) method that evaluates the predicate of a given argument.Syntaxpublic interface Predicate {  boolean test(T t); }Exampleimport java.util.*; import java.util.functionPredicate; public class LambdaPredicateTest {    public static void main(String args[]) {       Employee emp1 = new Employee("Raja", 26);       Employee emp2 = new Employee("Jaidev", 24);       Employee emp3 = new Employee("Adithya", 30);       List empList = new ArrayList();       empList.add(emp1);       empList.add(emp2); ... Read More

Maximum Number of Ones in C++

Arnab Chakraborty
Updated on 11-Jul-2020 12:36:39

143 Views

Suppose we have a matrix M with dimensions w x h, such that every cell has value 0 or 1, and any square sub-matrix of M of size l x l has at most maxOnes number of ones. We have to find the maximum possible number of ones that the matrix M can have.So, if the input is like w = 3, h = 3, l = 2, maxOnes = 1, then the output will be 4 as in a 3*3 matrix, no 2*2 sub-matrix can have more than 1 one. The best solution that has 4 ones is −101000101To ... Read More

Optimize Water Distribution in a Village Using Python

Arnab Chakraborty
Updated on 11-Jul-2020 12:34:17

712 Views

Suppose there are n houses in a village. We have to supply water for all the houses by building wells and laying pipes. For each house i, we can either build a well inside it, the building cost will be wells[i], or pipe in water from another well to it. The costs to lay pipes between houses are given by the array pipes, where each pipes[i] is [house1, house2, cost] represents the cost to connect house1 and house2 together using a pipe. These connections are bidirectional. We have to find the minimum total cost to supply water to all houses.So, ... Read More

Advertisements