How do you use the lymbda expression for "multi-made" interfaces?



  • Good night.

    There is a main method(s) in which the method run(s):

    public static void main(String args[]) {
        EventQueue.invokeLater(new Runnable() {
    
        @Override
        public void run() {
            new WorkTable().setVisible(true);
        }
    });
    

    }

    NetBeans proposed to replace anonymous lymbda-expression class:

    public static void main(String args[]) {
    EventQueue.invokeLater(() -> {
    new WorkTable().setVisible(true);
    });
    }

    Since the method run() is the only one in the Runnable interface, it is the way it is implemented. But how do we use the lymbda expression if the interface has several methods (e.g. List)?

    Thank you.



  • No. Documentation https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html that the lymbda of expression can only be used for interfaces with one method. In fact, the lymbda of expression is just syntax sugar (more readable and convenient syn) for anonymous classes. So, work a little old-fashioned (as in the first bundle of code).


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2