GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Java/java-lambda/README.md at master · 3208711/Java

Expand file tree

/

README.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
151 lines (107 loc) · 2.95 KB
/

README.md

Copy path

File metadata and controls

151 lines (107 loc) · 2.95 KB
Raw
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
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.