Found 9150 Articles for Object Oriented Programming

How to write a conditional expression in lambda expression in Java?

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

3K+ Views

The conditional operator is used to make conditional expressions in Java. It is also called a Ternary operator because it has three operands such as boolean condition,  first expression, and second expression.We can also write a conditional expression in lambda expression in the below program.Exampleinterface Algebra {    int substraction(int a, int b); } public class ConditionalExpressionLambdaTest {    public static void main(String args[]) {       System.out.println("The value is: " + getAlgebra(false).substraction(20, 40));       System.out.println("The value is: " + getAlgebra(true).substraction(40, 10));    }    static Algebra getAlgebra(boolean reverse) {       Algebra alg = reverse ... Read More

What is a casting expression in Java?

raja
Updated on 11-Jul-2020 07:40:37

849 Views

A cast expression provides a mechanism to explicitly provide a lambda expression's type if none can be conveniently inferred from context. It is also useful to resolve ambiguity when a method declaration is overloaded with unrelated functional interface types.SyntaxObject o = () -> { System.out.println("TutorialsPoint"); }; // Illegal: Object o = (Runnable) () -> { System.out.println("TutorialsPoint"); }; // LegalExampleinterface Algebra1 {    int operate(int a, int b); } interface Algebra2 {    int operate(int a, int b); } public class LambdaCastingTest {    public static void main(String[] args) {       printResult((Algebra1)(a, b) -> a + b);  // Cast Expression ... Read More

How to initialize an array using lambda expression in Java?

raja
Updated on 11-Jul-2020 07:31:06

2K+ Views

An array is a fixed size element of the same type. The lambda expressions can also be used to initialize arrays in Java. But generic array initializes cannot be used.Example-1interface Algebra {    int operate(int a, int b); } public class LambdaWithArray1 {    public static void main(String[] args) {       // Initialization of an array in Lambda Expression       Algebra alg[] = new Algebra[] {                                         (a, b) -> a+b,             ... Read More

How to use a return statement in lambda expression in Java?

raja
Updated on 11-Jul-2020 07:31:48

12K+ Views

A return statement is not an expression in a lambda expression. We must enclose statements in braces ({}). However, we do not have to enclose a void method invocation in braces. The return type of a method in which lambda expression used in a return statement must be a functional interface.Example 1public class LambdaReturnTest1 {    interface Addition {       int add(int a, int b);    }    public static Addition getAddition() {       return (a, b) -> a + b; // lambda expression return statement    }    public static void main(String args[]) {       System.out.println("The ... Read More

How to implement an action listener using method reference in Java?

raja
Updated on 11-Jul-2020 07:11:00

656 Views

In Java 8, lambda expression accepts an anonymous function as a parameter. In the case of providing an anonymous method, we can also pass references of existing methods using "::" symbol. A method reference enables us to do the same thing but with existing methods.We can also implement an action listener for a JButton by using static method reference and referred using the class name.Syntax:: Method-name;Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class MethodReferenceButtonListenerTest extends JFrame {    private JButton button;    public MethodReferenceButtonListenerTest() {       setTitle("Method Reference Button Listener");       button = new JButton("Method Reference");       ... Read More

Differences between Lambda Expression and Method Reference in Java?

raja
Updated on 11-Jul-2020 06:54:44

8K+ Views

Lambda expression is an anonymous method (method without a name) that has used to provide the inline implementation of a method defined by the functional interface while a method reference is similar to a lambda expression that refers a method without executing it. The arrow (->) operator can be used to connect the argument and functionality in a lambda expression while the (::) operator separates the method name from the name of an object or class in a method reference.Syntax for Lambda Expression([comma seperated argument-list]) -> {body}Syntax for Method Reference :: Exampleimport java.util.*; public class LambdaMethodReferenceTest {    public static void main(String args[]) { ... Read More

How to implement an instance method of an arbitrary object using method reference in Java?

raja
Updated on 11-Jul-2020 06:20:53

1K+ Views

A method reference is a new feature in Java 8, which is related to Lambda Expression. It can allow us to reference constructors or methods without executing them. The method references and lambda expressions are similar in that they both require a target type that consists of a compatible functional interface.SyntaxClass :: instanceMethodNameExampleimport java.util.*; import java.util.function.*; public class ArbitraryObjectMethodRefTest {    public static void main(String[] args) {       List persons = new ArrayList();       persons.add(new Person("Raja", 30));       persons.add(new Person("Jai", 25));       persons.add(new Person("Adithya", 20));       persons.add(new Person("Surya", 35));       persons.add(new ... Read More

How can we iterate the elements of List and Map using lambda expression in Java?

raja
Updated on 11-Jul-2020 06:15:14

11K+ Views

The lambda expressions are inline code that implements a functional interface without creating an anonymous class. In Java 8, forEach statement can be used along with lambda expression that reduces the looping through a Map to a single statement and also iterates over the elements of a list. The forEach() method defined in an Iterable interface and accepts lambda expression as a parameter.Example ( List using Lambda Expression)import java.util.*; public class ListIterateLambdaTest {    public static void main(String[] argv) {       List countryNames = new ArrayList();       countryNames.add("India");       countryNames.add("England");       countryNames.add("Australia");     ... Read More

JavaScript symbol.description property

AmitDiwan
Updated on 17-Dec-2019 11:03:49

107 Views

The symbol.description property in JavaScript converts a Symbol object to a primitive value. The syntax is as follows −Symbol()[Symbol.toPrimitive](hint);Example Live Demo Demo Heading Click to display... Result    function display() {       const val = Symbol('john');       console.log(val[Symbol.toPrimitive]);    } OutputClick the “Result” button −Example Live Demo Demo Heading Click to display... Result    function display() {       const val = Symbol(2465);       var res = val[Symbol.toPrimitive](99);       console.log(res)    } OutputClick the “Result” button and get the result in Console −

JavaScript Array.prototype.values()

AmitDiwan
Updated on 17-Dec-2019 10:58:48

183 Views

The array.values() method of JavaScript returns a new Array Iterator object that contains the values for each index in the array.The syntax is as follows −arr.values()Let us now implement the array.values() method in JavaScript −Example Live Demo Demo Heading Click the button to display the value... Result    function display() {       var arr = ['p', 'q', 'r'];       var res = arr.values();       document.getElementById("test").innerHTML = res.next().value    } OutputClick on the “Result” button −Example Live Demo Ranking Points Click to display the points... Result   ... Read More

Advertisements