Programming Articles - Page 2134 of 3366

How to implement ToLongFunction using lambda expression in Java?

raja
Updated on 14-Jul-2020 13:45:09

288 Views

ToLongFunction is a functional interface defined in java.util.function package. This functional interface accepts a reference type as input and produces a long-valued result. ToLongFunction interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsLong().Syntax@FunctionalInterface interface ToLongFunction {    long applyAsLong(T value); }Exampleimport java.util.*; import java.util.function.ToLongFunction; public class ToLongFunctionTest {    public static void main(String args[]) {       List list = new ArrayList();       list.add("11");       list.add("22");       list.add("33");       list.add("44");       list.add("55");       ToLongFunction function = (String item) -> Long.valueOf(item);  // ... Read More

How to implement ToDoubleBiFunction using lambda expression in Java?

raja
Updated on 14-Jul-2020 13:39:07

314 Views

ToDoubleBiFunction is a functional interface defined in java.util.function package. This functional interface accepts two parameters as input and produces a double-valued result. ToDoubleBiFunction interface can be used as an assignment target for a lambda expression or method reference. This interface contains only one abstract method: applyAsDouble() and doesn't contain any default or static methods.Syntax@FunctionalInterface interface ToDoubleBiFunction {    double applyAsDouble(T t, U u); }Exampleimport java.util.function.ToDoubleBiFunction; public class ToDoubleBiFunctionTest {    public static void main(String args[]) {       ToDoubleBiFunction test = (t, u) -> t / u;   // lambda expression       System.out.println("The division of t and u is: " + test.applyAsDouble(50, ... Read More

How to implement ToIntBiFunction using lambda expression in Java?

raja
Updated on 14-Jul-2020 13:40:19

293 Views

ToIntBiFunction is a built-in functional interface from java.util.function package. This interface accepts two parameters as input and produces an int-valued result. ToIntBiFunction interface can be used as an assignment target for a lambda expression or method reference. It contains only one abstract method: applyAsInt() and doesn't contain any default or static methods.Syntax@FunctionalInterface interface ToIntBiFunction {    int applyAsInt(T t, U u); }Exampleimport java.util.function.ToIntBiFunction; public class ToIntBiFunctionTest {    public static void main(String args[]) {       ToIntBiFunction test = (t, u) -> t * u;       System.out.println("The multiplication of t and u is: " + test.applyAsInt(10, 7));       System.out.println("The multiplication of t and u is: " ... Read More

How can we sort a Map by both key and value using lambda in Java?

raja
Updated on 14-Jul-2020 13:30:27

1K+ Views

A Map interface implements the Collection interface that provides the functionality of the map data structure. A Map doesn't contain any duplicate keys and each key is associated with a single value. We can able to access and modify values using the keys associated with them.In the below two examples, we can able to sort a Map by both key and value with the help of lambda expression.Example for sorting of Map using Keyimport java.util.*; import java.util.stream.*; public class MapSortUsingKeyTest {    public static void main(String args[]) {             // Sort a Map by their key       Map map = new HashMap(); ... Read More

How to implement LongUnaryOperator using lambda in Java?

raja
Updated on 14-Jul-2020 13:27:20

259 Views

LongUnaryOperator is a functional interface from java.util.function package. This functional interface accepts a single long-valued operand and produces a long-valued result. LongUnaryOperator interface can be used as an assignment target for lambda expression and method reference. It contains one abstract method: applyAsLong(), one static method: identity() and two default methods: andThen() and compose().Syntax@FunctionalInterface public interface LongUnaryOperator  long applyAsLong(long operand); }Exampleimport java.util.function.LongUnaryOperator; public class LongUnaryOperatorTest {    public static void main(String args[]) {       LongUnaryOperator getSquare = longValue -> {       // lambda          long result = longValue * longValue;          System.out.println("Getting square: " + result);   ... Read More

10 Reasons why you should Learn Python

Sharon Christine
Updated on 23-Jan-2020 05:21:27

2K+ Views

Python is a totally free language to download, use, and code. Its commands are mostly in simple English. This makes it easy to remember and write commands. The code is readable and with a little knowledge, a developer can learn many things just by looking at the code.It has standard libraries that offer a lot of functionalities which lets you implement complex applications with ease. Python was designed with the newbies in mind. The use of white space and common expressions has eliminated the need for tedious variable declarations and ugly braces.Your First Steps in ProgrammingPython can be your starting ... Read More

How to access variables from enclosing class using lambda in Java?

raja
Updated on 14-Jul-2020 13:00:28

801 Views

The variables defined by the enclosing scope of a lambda expression can be accessible within the lambda expression. A lambda expression can access to both instance, static variables and methods defined by the enclosing class. It has also access to "this" variable (both implicitly and explicitly) that can be an instance of enclosing class. The lambda expression also sets the value of an instance or static variable.Exampleinterface SimpleInterface {    int func(); } public class SimpleLambdaTest {    static int x = 50;    public static void main(String[] args) {       SimpleInterface test = () -> x;     // Accessing of static variable ... Read More

Print a pattern without using any loop in C++

sudhir sharma
Updated on 22-Jan-2020 12:44:25

711 Views

In this problem, we are given a number n. Our task is to print patterns with decreasing to 0 or negative then increasing back to the number.Let’s take an example to understand the problem,Input: n = 12 Output: 12 7 2 -3 2 7 12To solve this problem, we will use recursion and call the function after each update. The track of update is kept using the flag variable which tells the function to increase or decrease the number by 5.ExampleThe below code gives the implementation of our solution, Live Demo#include using namespace std; void printNextValue(int m){    if (m > 0){       cout

Print a String in wave pattern in C++

sudhir sharma
Updated on 22-Jan-2020 12:42:59

546 Views

In this problem, we are given a string and an integer n. Our task is to print the given string in a wave pattern of n lines.Let’s take an example to understand the problem, Input: Tutorial n = 3 Output: T             r    U       o       i       s       t                lWave patterns are printed by printing each character of the string one by one in the next line and tab space away from the next element till ... Read More

Print all 3 digit repeating numbers in a very large number in C++

sudhir sharma
Updated on 22-Jan-2020 12:38:07

319 Views

In this problem, we are given a number. And we have to print all 3 digit repeating numbers.Let’s take an example to understand the problem, Input: 98769876598765 Output:    987: 3 times    876: 3 times    765: 2 timesTo solve this problem, we will use the large number which is stored string. The digits of the numbers are counted as characters. Now, we will check the first three numbers and then start from the 3rd index to the end and get a new number. After this, we will check for the next 3 digit numbers are count its frequency. ... Read More

Advertisements