Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
151 lines (107 loc) · 2.95 KB

File metadata and controls

151 lines (107 loc) · 2.95 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

Using Lambda to Implement Functional Interface Method

  • We have a virtual Button that when clicked will call an abstract method onClick() of a listener interface OnClickListener.
  • We need to implement the onClick() so that it prints the Button name.

OnClickListener.java

package com.hmkcode;

public interface OnClickListener {
	void onClick(Button button);
}

Button.java

package com.hmkcode;

public class Button {

	private OnClickListener onClickListener;
	private String name;

	// click the button
	public void click(){
		this.onClickListener.onClick(this);
	}
	
	// getters & setters
}
  • We have three ways to achieve that:
  1. Implement OnClickListener and override onClick() method.
  2. Use OnClickListener as an anonymous class.
  3. Use Lambda.
  • Our main class is App.java
package com.hmkcode;

public class App
{
    public static void main( String[] args ){
        System.out.println( "Running App..." );
        new App().run(); 
    }

    public void run(){
        
        Button myButton = new Button();
        myButton.setName("MyButton");
        
        // 1. implements onClickListener
        
        // 2. anonymous class

        // 3. lambda
    	
	
    	// click the button
    	myButton.click();
    }
}

1. Implement OnClickListener and override onClick() method

public class App implements OnClickListener
{
    public static void main( String[] args ){...}

    public void run(){
        
        Button myButton = new Button();
        myButton.setName("MyButton");
        
        // 1. implements onClickListener
        myButton.setOnClickListener(this);
        
        // click the button
    	myButton.click();
    }
	
	@Override
	public void onClick(Button button) {
		System.out.println(button.getName() +" Clicked! - implements interface");
		
	}
}

2. Use OnClickListener as an anonymous class

public class App
{
    public static void main( String[] args ){...}

    public void run(){
        
        Button myButton = new Button();
        myButton.setName("MyButton");
                
        // 2. anonymous class
        myButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(Button button) {
				System.out.println(button.getName() +" Clicked! - anonymous class");
			}
		}); 
        
        // click the button
    	myButton.click();
    }
}

3. Use Lambda

public class App
{
    public static void main( String[] args ){...}

    public void run(){
        
        Button myButton = new Button();
        myButton.setName("MyButton");

        // 3. lambda
    	OnClickListener lambda = button -> { System.out.println(button.getName()+" Clicked! - lambda"); } ;
    	myButton.setOnClickListener(lambda);
		
        // click the button
    	myButton.click();
    } 
}

To run the code use

java-lambda>mvn exec:java
Morty Proxy This is a proxified and sanitized view of the page, visit original site.