package chapter6;

import java.util.*;
import javax.swing.*;
import javax.swing.Timer;

public class LambaTest {
    public static void main(String[] args) {

        var planets = new String[] { "Mercury", "Venus", "Earth", "Mars","Jupiter", "Saturn", "Uranus", "Neptune" };
        System.out.println(Arrays.toString(planets));
        System.out.println("Sorted in dictionary order:");
        Arrays.sort(planets);
        System.out.println(Arrays.toString(planets));
        System.out.println("Sorted by length:");
        Arrays.sort(planets, (first, second) -> first.length() - second.length());
        System.out.println(Arrays.toString(planets));
        var timer = new Timer(1000, event ->
                System.out.println("The time is "+ new Date()));
        timer.start();
// keep program running until user selects "0K'
        JOptionPane.showMessageDialog(null, "Quit program?");
        System.exit(0);
    }
}

打印如下:

lambda实现比较器和动作监听器-LMLPHP

04-07 14:40