Question

In: Computer Science

Using Java, Explain how to implement a functional interface using a lambda expression. You may include...

Using Java, Explain how to implement a functional interface using a lambda expression. You may include small sections of code to illustrate your explanation.

Solutions

Expert Solution

Functional interface is an interface that has only one abstract method.

Lamda expression is used to represent the instance of functional interface.

Means using lamda expression we can write definition of an abstract method.

Suppose if we have a functional interface square,

@FunctionalInterface
interface Square
{
int calculateSquare(int x);
}

We can use @FunctionalInterface annotaton to functional interface but it is optional. Above functional interface has one abstract method calculateSquare.

To define the implementation of that method, we can write lamda expression as below :

Square s = (int x) -> x*x;

Means it accepts one parameter and performs square of it.

//functional interface that has only one abstract method
@FunctionalInterface
interface Square 
{ 
    int calculateSquare(int x); 
} 

public class Main
{
        public static void main(String[] args) {
            
                //lambda expression to define the calculateSquare method 
                //means implementation of calculateSquare method.
        Square s = (int x) -> x*x; 
        
        // parameter passed and return type must be 
        // same as defined in the prototype 
        int ans = s.calculateSquare(5); 
        System.out.println(ans); 
        }
}

Please refer below screenshot of code for indentation and output.


Related Solutions

This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface called EmployeeInfo with the following constant variables: FACULTY_MONTHLY_SALARY = 6000.00 STAFF_MONTHLY_HOURS_WORKED = 160 2. Implement an abstract class Employee with the following requirements: Attributes last name (String) first name (String) ID number (String) Sex - M or F Birth date - Use the Calendar Java class to create a date object Default argument constructor and argument constructors. Public methods toString - returning a string...
can you teach me how to implement the ArrayStringList and LinkedStringList using the same interface? I've...
can you teach me how to implement the ArrayStringList and LinkedStringList using the same interface? I've implemented a couple of methods in my ArrayStringList but I'm not sure if I'm doing it correctly and im confused on how to do the splice, reverse, and a couple others. I'm also having trouble getting started on my LinkedStringList. In this project, you will be providing two different implementations of a StringList interface, which defines different operations that one should be able to...
Design a Java program named LambdaTester2 which includes a main method that declares a lambda expression...
Design a Java program named LambdaTester2 which includes a main method that declares a lambda expression reference which implements the following interface: interface Multiplier {     int multiply(int num1, int num2); } The program must declare an array of Pair objects (see class specification below), followed by the declaration of an ArrayList which is instantiated using the declared array (the ArrayList stores Pair objects). Then write a loop which iterates through each element of the ArrayList, calls the lambda expression reference...
write code in java and comment. thanks. the program is about interface . Implement the basics...
write code in java and comment. thanks. the program is about interface . Implement the basics of Fitness and types of Fitness: Aerobic. Implement specific Fitness types such as Swimming, Cycling, Fitness Task: public interface Fitness (10pts) This will be used as a starting point for deriving any specific Fitness type. Every fitness exercise type has one or more muscle group it affects. Therefor a Fitness has the following abstarct method (Note that all methods in an interface are abstract...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Explain what an activator is and how such proteins can regulate gene expression. You should include...
Explain what an activator is and how such proteins can regulate gene expression. You should include in your answer details of the genetic elements activators can bind to and outline two different molecular mechanisms by which activators can influence gene expression
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
Instructions: SLLQueue (13 pts) ● Using the three properties below, implement the queue interface using the...
Instructions: SLLQueue (13 pts) ● Using the three properties below, implement the queue interface using the SLLNode class from Lab 2: ○ head_node → SLLNode object or None ○ tail_node → SLLNode object or None ○ size → int, keep track of queue size ● Implement enqueue( ), dequeue( ), front( ) ● Use SLLNode methods: get_item( ), set_item( ), get_next( ), set_next( ) ● (5 pts) In enqueue(item): ○ Add new SLLNode (with item) after tail_node ○ Update tail_node...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    *...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    * Gets the current number of entries in this set.    *    * @return The integer number of entries currently in the set.    */    public int getSize();    /**    * Sees whether this set is empty.    *    * @return True if the set is empty, or false if not.    */    public boolean isEmpty();    /**    *...
Bus ticket reservation project in java using class ,inheritance,interface
Bus ticket reservation project in java using class ,inheritance,interface
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT