Skip to main content

WorldClock and Lambdas

The second Java 8 feature to use in WorldClock are the Lambdas.

The simplest way to start using them seems to apply Netbeans refactoring hints.

These hints are of two kinds for WorldClock:

  • Iterations:

PreJava8:

for (City city : cities) {
   city.paint(g, width, height, isFullScreen);
}

Java8:

cities.stream().forEach((city) -> {
    city.paint(g, width, height, isFullScreen);
});

It may not bring much in this instance though.

  • Single method anonymous inner classes:

PreJava8:

mnuiShow.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e)       {
        showWindow();
      }
});

Java8:

mnuiShow.addActionListener((ActionEvent e) -> {
   showWindow();
});

There it does reduce the boiler plate code.