Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| codding:java8_lambda_use_cases [2014/08/21 08:40] – yehuda | codding:java8_lambda_use_cases [2022/01/03 16:03] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 34: | Line 34: | ||
| } | } | ||
| </ | </ | ||
| + | |||
| + | ===== Listener ===== | ||
| + | Java Way | ||
| + | <code java> | ||
| + | public class ListenerTest { | ||
| + | public static void main(String[] args) { | ||
| + | | ||
| + | JButton testButton = new JButton(" | ||
| + | | ||
| + | testButton.addActionListener(new ActionListener(){ | ||
| + | @Override public void actionPerformed(ActionEvent ae){ | ||
| + | System.out.println(" | ||
| + | } | ||
| + | }); | ||
| + | | ||
| + | // Swing stuff | ||
| + | JFrame frame = new JFrame(" | ||
| + | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | frame.add(testButton, | ||
| + | frame.pack(); | ||
| + | frame.setVisible(true); | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Lambda Way | ||
| + | <code java> | ||
| + | public class ListenerTest { | ||
| + | public static void main(String[] args) { | ||
| + | | ||
| + | JButton testButton = new JButton(" | ||
| + | testButton.addActionListener(e -> System.out.println(" | ||
| + | | ||
| + | // Swing stuff | ||
| + | JFrame frame = new JFrame(" | ||
| + | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
| + | frame.add(testButton, | ||
| + | frame.pack(); | ||
| + | frame.setVisible(true); | ||
| + | | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ===== Improve Code ===== | ||
| + | Java Way | ||
| + | <code java> | ||
| + | public class Person { | ||
| + | new Thread(new Runnable() { | ||
| + | @Override | ||
| + | public void run() { | ||
| + | System.out.println(" | ||
| + | } | ||
| + | }).start(); | ||
| + | </ | ||
| + | |||
| + | Lambda Way | ||
| + | <code java> | ||
| + | new Thread( | ||
| + | () -> System.out.println(" | ||
| + | ).start(); | ||
| + | </ | ||
| + | |||
| + | ===== Foreach ===== | ||
| + | Define: | ||
| + | <code java> | ||
| + | List< | ||
| + | </ | ||
| + | |||
| + | Java Way | ||
| + | <code java> | ||
| + | for(Integer n: list) { | ||
| + | System.out.println(n); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Lambda Way | ||
| + | <code java> | ||
| + | list.forEach(n -> System.out.println(n)); | ||
| + | </ | ||
| + | |||
| + | Java8 Way | ||
| + | <code java> | ||
| + | list.forEach(System.out:: | ||
| + | </ | ||
| + | |||
| + | ===== Foreach and Multiply ===== | ||
| + | <code java> | ||
| + | List< | ||
| + | </ | ||
| + | |||
| + | Java Way | ||
| + | <code java> | ||
| + | for(Integer n : list) { | ||
| + | int x = n * n; | ||
| + | System.out.println(x); | ||
| + | } | ||
| + | </ | ||
| + | Lambda Way | ||
| + | <code java> | ||
| + | list.stream().map((x) -> x*x).forEach(System.out:: | ||
| + | </ | ||
| + | |||
| + | ===== MapReduce in java===== | ||
| + | <code java> | ||
| + | List< | ||
| + | </ | ||
| + | Java Way | ||
| + | <code java> | ||
| + | int sum = 0; | ||
| + | for(Integer n : list) { | ||
| + | int x = n * n; | ||
| + | sum = sum + x; | ||
| + | } | ||
| + | System.out.println(sum); | ||
| + | </ | ||
| + | Lambda Way | ||
| + | <code java> | ||
| + | int sum = list.stream().map(x -> x*x).reduce((x, | ||
| + | System.out.println(sum); | ||
| + | </ | ||
| + | |||
| + | |||