Created
July 27, 2012 11:58
-
-
Save skrb/3187589 to your computer and use it in GitHub Desktop.
JJUG Project Lambda Hands-on Materials - Sample Answers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class FizzBuzzWLambda { | |
private static final String FIZZBUZZ = "FizzBuzz"; | |
private static final String FIZZ = "Fizz"; | |
private static final String BUZZ = "Buzz"; | |
public FizzBuzzWLambda() { | |
List<Integer> numbers = initNumbers(); | |
numbers.forEach(e -> { | |
if (e%15 == 0) { | |
System.out.println(FIZZBUZZ); | |
} else if (e%3 == 0) { | |
System.out.println(FIZZ); | |
} else if (e%5 == 0) { | |
System.out.println(BUZZ); | |
} else { | |
System.out.println(e); | |
} | |
}); | |
} | |
private List<Integer> initNumbers() { | |
List<Integer> numbers = new ArrayList<>(); | |
for (int i = 0; i <= 100; i++) { | |
numbers.add(i); | |
} | |
return numbers; | |
} | |
public static void main(String... args) { | |
new FizzBuzzWLambda(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.functions.Mapper; | |
public class NumberAdder { | |
Mapper<List<Integer>, Integer> mapper; | |
public NumberAdder() { | |
List<Integer> numbers = initNumbers(); | |
// 再帰を利用したループ処理 | |
mapper | |
= nums -> (nums.size() <= 1) ? nums.get(0): nums.remove(0) + mapper.map(nums); | |
Integer sum = mapper.map(numbers); | |
System.out.println(sum); | |
} | |
private List<Integer> initNumbers() { | |
List<Integer> numbers = new LinkedList<>(); | |
for (int i = 1; i <= 10; i++) { | |
numbers.add(i); | |
} | |
return numbers; | |
} | |
public static void main(String... args) { | |
new NumberAdder(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Arrays; | |
import java.util.Random; | |
public class ParallelScoreFinder { | |
private Random random = new Random(); | |
public ParallelScoreFinder() { | |
long[] serialTimes = new long[50]; | |
long[] parallelTimes = new long[50]; | |
int maxIndex = 0; | |
int minIndex = 0; | |
long max = 0; | |
long min = 1_000_000_000_000_000L; | |
// Warm up | |
for (int i = 0; i < 20; i++) { | |
findMaxScore(); | |
} | |
for (int i = 0; i < 50; i++) { | |
serialTimes[i] = findMaxScore(); | |
if (serialTimes[i] > max) { | |
maxIndex = i; | |
} else if (serialTimes[i] < min) { | |
minIndex = i; | |
} | |
} | |
long sum = 0; | |
int index = 0; | |
for (int i = 0; i < 50; i++) { | |
if (i == minIndex) { | |
continue; | |
} | |
if (i == maxIndex) { | |
continue; | |
} | |
sum += serialTimes[i]; | |
} | |
System.out.println(index); | |
System.out.println("Serial Time: " + sum/48/1_000_000); | |
maxIndex = 0; | |
minIndex = 0; | |
max = 0; | |
min = 1_000_000_000_000_000L; | |
// Warm up | |
for (int i = 0; i < 20; i++) { | |
findMaxScoreParallel(); | |
} | |
for (int i = 0; i < 50; i++) { | |
parallelTimes[i] = findMaxScoreParallel(); | |
if (parallelTimes[i] > max) { | |
maxIndex = i; | |
} else if (parallelTimes[i] < min) { | |
minIndex = i; | |
} | |
} | |
sum = 0; | |
for (int i = 0; i < 50; i++) { | |
if (i == minIndex) { | |
continue; | |
} | |
if (i == maxIndex) { | |
continue; | |
} | |
sum += parallelTimes[i]; | |
} | |
System.out.println("Parallel Time: " + sum/48/1_000_000); | |
} | |
private long findMaxScore() { | |
Student[] students = initStudents(); | |
long start = System.nanoTime(); | |
Integer maxScore | |
= Arrays.asList(students) | |
.filter(s -> s.getGradYear() == 2011) | |
.map(Student::getScore) | |
.reduce(0, Math::max); | |
long end = System.nanoTime(); | |
return end - start; | |
} | |
private long findMaxScoreParallel() { | |
Student[] students = initStudents(); | |
long start = System.nanoTime(); | |
Integer maxScore | |
= Arrays.parallel(students) | |
.filter(s -> s.getGradYear() == 2011) | |
.map(Student::getScore) | |
.reduce(0, Math::max); | |
long end = System.nanoTime(); | |
return end - start; | |
} | |
private Student[] initStudents() { | |
Student[] students = new Student[5_000_001]; | |
for (int i = 0; i <= 5_000_000; i++) { | |
students[i] = new Student(""+i, random.nextInt(12)+2000, random.nextInt(101)); | |
} | |
return students; | |
} | |
public static void main(String... args) { | |
new ParallelScoreFinder(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Random; | |
public class ScoreFinder { | |
private Random random = new Random(); | |
public ScoreFinder() { | |
List<Student> students = initStudents(); | |
Integer maxScore | |
= students.filter(s -> s.getGradYear() == 2011) | |
.map(Student::getScore) | |
.reduce(0, Math::max); | |
// いずれで書いてもOK | |
/* | |
Integer maxScore | |
= students.filter(s -> s.getGradYear() == 2011) | |
.map(s -> s.getScore()) | |
.reduce(0, (s1, s2) -> Math.max(s1, s2)); | |
*/ | |
/* | |
Integer maxScore | |
= students.filter(s -> s.getGradYear() == 2011) | |
.map(s -> s.getScore()) | |
.reduce(0, (s1, s2) -> s1>s2? s1: s2); | |
*/ | |
System.out.println(maxScore); | |
} | |
private List<Student> initStudents() { | |
List<Student> students = new ArrayList<>(); | |
for (int i = 0; i <= 5_000_000; i++) { | |
students.add(new Student(""+i, random.nextInt(12)+2000, random.nextInt(101))); | |
} | |
return students; | |
} | |
public static void main(String... args) { | |
new ScoreFinder(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Student { | |
private String name; | |
private int gradYear; | |
private int score; | |
public Student(String name, int gradYear, int score) { | |
this.name = name; | |
this.gradYear = gradYear; | |
this.score = score; | |
} | |
public String getName() { | |
return name; | |
} | |
public int getGradYear() { | |
return gradYear; | |
} | |
public int getScore() { | |
return score; | |
} | |
public String toString() { | |
return "["+name+", year:" + gradYear + ", score:" + score + "]"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.BorderLayout; | |
import java.awt.FlowLayout; | |
import java.awt.event.ActionListener; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JPanel; | |
import javax.swing.JTextField; | |
import javax.swing.SwingUtilities; | |
public class SwingDemoWLambda { | |
public SwingDemoWLambda() { | |
JFrame frame = new JFrame("Swing Demo"); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setSize(400, 150); | |
JPanel panel = new JPanel(); | |
panel.setLayout(new FlowLayout(FlowLayout.CENTER, 20, 20)); | |
// 実施的 final | |
JTextField field = new JTextField(20); | |
panel.add(field); | |
JButton button = new JButton("Update"); | |
panel.add(button); | |
frame.add(panel, BorderLayout.NORTH); | |
// 実施的 final | |
JLabel label = new JLabel(); | |
label.setHorizontalAlignment(JLabel.CENTER); | |
frame.add(label, BorderLayout.CENTER); | |
// field も label も実質的 final であるため、Lambda 式でアクセス可能 | |
ActionListener listener | |
= event -> { label.setText(field.getText()); }; | |
field.addActionListener(listener); | |
button.addActionListener(listener); | |
frame.setVisible(true); | |
} | |
public static void main(String... args) { | |
// 通常の Lambda 式 | |
// SwingUtilities.invokeLater(() -> { new SwingDemoWLambda(); }); | |
// コンストラクタ参照を使用した場合 | |
SwingUtilities.invokeLater(SwingDemoWLambda::new); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment