Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
codding:java8_lambda_use_cases [2014/08/21 07:52] – created yehuda | codding:java8_lambda_use_cases [2022/01/03 16:03] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Java8 Lambda Use cases ====== | ====== Java8 Lambda Use cases ====== | ||
+ | |||
+ | ===== Runnable ===== | ||
+ | Instead using: | ||
+ | <code java> | ||
+ | public class RunnableTest { | ||
+ | public static void main(String[] args) { | ||
+ | System.out.println(" | ||
+ | | ||
+ | Runnable r = new Runnable(){ | ||
+ | | ||
+ | @Override | ||
+ | public void run(){ | ||
+ | System.out.println(" | ||
+ | } | ||
+ | }; | ||
+ | | ||
+ | r.run(); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | Use: | ||
+ | <code java> | ||
+ | public class RunnableTest { | ||
+ | public static void main(String[] args) { | ||
+ | | ||
+ | System.out.println(" | ||
+ | |||
+ | Runnable r = () -> System.out.println(" | ||
+ | | ||
+ | r.run(); | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== 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); | ||
+ | </ | ||
+ | |||
+ |