Remember what Lambda is short for. It's simply used for method definitions, particularly for functional interfaces. Pretty much wherever you can use an object where you define some function (with simple logic), you could also easily use Lambda. A good example to use would be ActionListener.
Without lambda, we could say:
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
};
With lambda, we can shorten this to the simple expression:
ActionListener al = e -> System.out.println(e.getActionCommand());
It's clear to see that these are directly related to each other; 'ActionEvent e', from the original, is simply declared as 'e' in the secondary, as the compiler already knows what type it's of, and similarly, we can define the body of the method by declaring what it does in a singular line.
Now, these can also be passed as parameters, because they're just like any other object. For instance, let's say we had a JButton object, which frequently is given an ActionListener. Note that JButton has a method JButton#addActionListener(ActionListener al). Now, we can pass our ActionListener in any of the following ways:
JButton b = new JButton();
//Option One
ActionListener al = e -> System.out.println(e.getActionCommand());
b.addActionListener(al);
//Option Two
b.addActionListener(e -> System.out.println(e.getActionCommand()));
//Option Three
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
});
//Option Four
ActionListener al = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(e.getActionCommand());
}
};
b.addActionListener(al);
All of these are logically equivalent!