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 at master · SamerHAlimoor/Java

Expand file tree

/

java-lambda

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

java-lambda

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
src/main/java/com/hmkcode
src/main/java/com/hmkcode
 
 
README.md
README.md
 
 
pom.xml
pom.xml
 
 

README.md

Outline

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.