Slide 1

Slide 1 text

No content

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

• • • •

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

• • • • • •

Slide 15

Slide 15 text

• • • • • •

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

1 2 3 4 5 • • • • • • • • •

Slide 22

Slide 22 text

• • • • • •

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

final Class a = List.class; final Class b = String.class; final Class c = double.class; final Class d = int[][].class; • •

Slide 25

Slide 25 text

import java.awt.Event; public class MyApp extends java.applet.Applet { public boolean action(Event evt, Object arg) { if (evt.target == APPROVE_BUTTON) { return doSomething1(evt, arg); } else if (evt.target == REJECT_BUTTON) { return doSomething2(evt, arg); } else if (evt.target == DATE_CHOICE) { return doSomething3(evt, arg); } … return super.action(evt, arg); } import java.awt.event.*; public class MyApp2 extends java.applet.Applet { public void init() { this.addMouseListener(new MyMouseAdapter()); Button approveButton = new Button("承認"); approveButton.addActionListener(new ApproveListner()); this.add(approveButton); } }

Slide 26

Slide 26 text

import java.awt.event.*; public class MyApp2 extends java.applet.Applet { public void init() { this.addMouseListener(new MyMouseAdapter()); Button approveButton = new Button("承認"); approveButton.addActionListener(new ApproveListner()); this.add(approveButton); } } import java.awt.event.*; public class ApproceListner implements ActionListener { public void actionPerformed(final ActionEvent e) { // 承認 // 承認 // 承認 } }

Slide 27

Slide 27 text

import java.awt.event.*; public class MyApp3 extends java.applet.Applet { public class ApproceListner implements ActionListener { public void actionPerformed(final ActionEvent e) { // 承認 // 承認 // 承認 } } public void init() { this.addMouseListener(new MyMouseAdapter()); Button approveButton = new Button("承認"); approveButton.addActionListener(new ApproveListner()); this.add(approveButton); } } • • •

Slide 28

Slide 28 text

import java.awt.event.*; public class MyApp3 extends java.applet.Applet { public class ApproceListner implements ActionListener { public void actionPerformed(final ActionEvent e) { // 承認 // 承認 // 承認 } } public void init() { this.addMouseListener(new MyMouseAdapter()); Button approveButton = new Button("承認"); approveButton.addActionListener(new ApproveListner()); this.add(approveButton); } }

Slide 29

Slide 29 text

import java.awt.event.*; public class MyApp4 extends java.applet.Applet { public void init() { this.addMouseListener(new MyMouseAdapter()); Button approveButton = new Button("承認"); approveButton.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { // 承認 // 承認 // 承認 } }); this.add(approveButton); } } • • •

Slide 30

Slide 30 text

import java.awt.event.*; public class MyApp4 extends java.applet.Applet { public void init() { this.addMouseListener(new MyMouseAdapter()); Button approveButton = new Button("承認"); approveButton.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { // 承認 // 承認 // 承認 } }); this.add(approveButton); } }

Slide 31

Slide 31 text

import java.awt.event.*; public class MyApp4 extends java.applet.Applet { public void init() { this.addMouseListener(new MyMouseAdapter()); Button approveButton = new Button("承認"); approveButton.addActionListener({ // 承認 // 承認 // 承認 }); this.add(approveButton); } } 結果 = sort(users, 名前でソート); 結果 = sort(users, 年齢でソート); 結果 = sort(users, 所属部署でソート); 結果 = filter(users, 名古屋に住んでいる); 結果 = filter(users, 関数型言語が好きである); 結果 = filter(users, リア充である); 結果 = transform(users, 名前を大文字に); 結果 = transform(users, 給与を半分に);

Slide 32

Slide 32 text

• • • •

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

1 2 3 4 5 • • • • • • • • • • • • • • • •

Slide 40

Slide 40 text

• • •

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

1 2 3 4 5 • • • • • • • • •

Slide 47

Slide 47 text

• • •

Slide 48

Slide 48 text

• • •

Slide 49

Slide 49 text

List list = (List) Proxy.newProxyInstance(List.class.getClassLoader(), new Class[]{ List.class }, new MyInvocationHandler());

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

1 2 3 4 5 • • • • • • • • • • • • • • • • • • • • • • • • • •

Slide 54

Slide 54 text

• • • • •

Slide 55

Slide 55 text

• • try { doSomething(); } catch (LowLevelException cause) { throw new HighLevelException( "●●が▲▲です", cause); }

Slide 56

Slide 56 text

• • • • • • • • • • • • • • • • •

Slide 57

Slide 57 text

• • • • • •

Slide 58

Slide 58 text

• •

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

• • • • • •

Slide 61

Slide 61 text

• • • • Charset sjis = Charset.forName("Shift-JIS"); InputStream is = new FileInputStream(new File("hoge.txt")); Reader reader = new BufferedReader(new InputStreamReader(is, sjis));

Slide 62

Slide 62 text

• • • • • • • •

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

1 2 3 4 5 • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Slide 66

Slide 66 text

List names = ……; names.add(9999); for (int i = 0; i < names.size(); i++) { String name = (String) names.get(i); System.out.println(name); }

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

• • List names = ……; for (int i = 0; i < names.size(); i++) { // まじこわいキャスト String name = (String) names.get(i); System.out.println(name); } // 中身なんて知ったことか!! names.add(9999); List names = ……; for (int i = 0; i < names.size(); i++) { String name = names.get(i); System.out.println(name); } names.add(9999);

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

• • • • •

Slide 72

Slide 72 text

int pri = 1; Integer b = Integer.valueOf(pri); int pri = 1; Integer b = pri;

Slide 73

Slide 73 text

@Test(expected = NoSuchElementException.class) @Ignore public void getShouldFailIfEmtpy() { Optional.empty().get(); }

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

public enum Imoni { YAMAGATA, FUKUSHIMA, MIYAGI, MIX; } public enum Suite { SPADE, HEART, CLUB, DIAMOND; } public enum Janken { ROCK, PAPER, SCISSOR; } public enum JapaneseProvince { TOKYO, HOKKAIDO, … AICHI, … OKINAWA } assert Suite.SPADE != Suite.HEART; assert Suite.SPADE != Janken.ROCK;

Slide 76

Slide 76 text

• • • public enum Planet { MERCURY(3.303e+23, 2.4397e6), VENUS(4.869e+24, 6.0518e6), EARTH(5.976e+24, 6.37814e6), MARS(6.421e+23, 3.3972e6), JUPITER(1.9e+27, 7.1492e7), SATURN(5.688e+26, 6.0268e7), URANUS(8.686e+25, 2.5559e7), NEPTUNE(1.024e+26, 2.4746e7); private final double mass; private final double radius; Planet(double mass, double radius) { this.mass = massKillogram; this.radius = radiusMeter; } public static final double G = 6.67300E-11; public double surfaceGravity() { return G * mass / (radius * radius); }

Slide 77

Slide 77 text

Set 全部 = EnumSet.allOf(Planet.class); Set 地球以外 = EnumSet.complementOf(EnumSet.of(EARTH)); Map 芋煮種人気 = new EnumMap(Imoni.class); 芋煮種人気.put(Imoni.FUKUSHIMA, 1024); 芋煮種人気.put(Imoni.YAMAGATA, 256); 芋煮種人気.put(Imoni.MIX, -9999);

Slide 78

Slide 78 text

List src = Arrays.asList("Java", "Scala", "Clojure"); Set langs = new HashSet<>(src); Iterator iterator = langs.iterator(); while (iterator.hasNext()) { String e = iterator.next(); System.out.println(e); } Iterator iterator2 = langs.iterator(); for (String e; iterator2.hasNext(); ) { e = iterator2.next(); System.out.println(e); } String[] array = {"Ruby", "Python", "JavaScript"}; for (int i = 0; i < array.length; i++) { String e = array[i]; System.out.println(e); } for (final String e : langs) { System.out.println(e); } for (final String e : array) { System.out.println(e); }

Slide 79

Slide 79 text

No content

Slide 80

Slide 80 text

Maybe maybeName = …; Maybe maybeAge = …; for (String name : maybeName) { for (int age : maybeAge) { return String.format("%sさんは%s歳です", name, age); } } return "そんな人はいないか、年齢が分かりません!!";

Slide 81

Slide 81 text

public static int sum(int... ints) { int sum = 0; for (int i : ints) { sum += i; } return sum; } public static void main(String... args) { System.out.println(sum(1)); System.out.println(sum(2, 3, 4)); System.out.println(sum(new int[]{5, 6})); }

Slide 82

Slide 82 text

import static java.lang.Math.random; import static java.lang.Math.floor; public class NewStyle { public static void main(String[] args) { double x = floor(random() * 100); System.out.println(x); } } public class FormerStyle { public static void main(String[] args) { double d = Math.floor(Math.random() * 100); System.out.println(d); } }

Slide 83

Slide 83 text

List mutableList = newArrayList("Java", "Scala", "Clojure"); Set mutableSet = newHashSet("Java", "Scala", "Clojure");

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

1 2 3 4 5 • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Slide 89

Slide 89 text

• • •

Slide 90

Slide 90 text

// 先頭からn件 final String[] original = {"Java", "Scala", "F#", "C#", "Haskell", "Python"}; final String[] first3 = Arrays.copyOf(original, 3); assertThat(first3, is(new String[]{"Java", "Scala", "F#"})); // i番目~j-1番目まで final int[] original2 = {0, 10, 20, 30, 40, 50}; final int[] range = Arrays.copyOfRange(original2, 3, 5); assertThat(range, is(new int[]{30, 40}));

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

• • • • • •

Slide 93

Slide 93 text

@Override @SuppressWarnings("unchecked") public void actionPerformed(final ActionEvent e) { return (Class) foo.getClass(); }

Slide 94

Slide 94 text

• •

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

No content

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

No content

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

1 2 3 4 5 • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Slide 104

Slide 104 text

• • • • • • •

Slide 105

Slide 105 text

ZipFile zf = new ZipFile(zipFileName); try { BufferedWriter writer = Files.newBufferedWriter(outputPath, charset); try { for (ZipEntry entry : Collections.list(zf.entries())) { String entryName = entry.getName() + newLine; writer.write(entryName, 0, entryName.length()); } } finally { writer.close(); } } finally { zf.close(); } try (ZipFile zf = new ZipFile(zipFileName); BufferedWriter writer = Files.newBufferedWriter(outputPath, charset)) { for (ZipEntry entry : Collections.list(zf.entries())) { String entryName = entry.getName() + newLine; writer.write(entryName, 0, entryName.length()); } }

Slide 106

Slide 106 text

List list = new ArrayList(); Map> bag; bag = new HashMap>(); List list = new ArrayList<>(); Map> bag; bag = new HashMap<>();

Slide 107

Slide 107 text

String[] array = {"おおさか", "なごや", "ふくおか"}; for (String name : array) { switch (name) { case "なごや": System.out.println("こわい"); break; default: System.out.println("こわくない"); } } • • •

Slide 108

Slide 108 text

try { FileInputStream is = new FileInputStream(new File("")); Document doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(is); } catch (IOException e) { // 例外処理 } catch (ParserConfigurationException e) { // 例外処理 } catch (SAXException e) { // 例外処理 } try { FileInputStream is = new FileInputStream(new File("")); Document doc = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(is); } catch (IOException ex) { // IO系例外処理 } catch (ParserConfigurationException | SAXException ex) { // XML系例外処理 } • •

Slide 109

Slide 109 text

final int[] phases = { 0b00110001, 0b01100010, 0b11000100, 0b10001001, 0b00010011, 0b00100110, 0b01001100, 0b10011000 }; final int[] phases = { Integer.parseInt("00110001", 2), Integer.parseInt("01100010", 2), Integer.parseInt("11000100", 2), Integer.parseInt("10001001", 2), Integer.parseInt("00010011", 2), Integer.parseInt("00100110", 2), Integer.parseInt("01001100", 2), Integer.parseInt("10011000", 2) }; int bin = 0b00_11_01_01_01_01; int oct = 0123_4567; int hex = 0xCAFE_BABE;

Slide 110

Slide 110 text

• • • • • List list = #[1, 2, 3]; Set set = #[1, 2, 3]; Map map = #{ "foo": 1, "bar": 2 }; int[] array = {1, 2, 3};

Slide 111

Slide 111 text

public Foo(Object bar, Object[] buz) { this.bar = Objects.requireNonNull(bar); this.buz = Objects.requireNonNull(buz, "buzはnullダメ"); } @Override public boolean equals(Object o) { if (o == null || o instanceof Foo) return false; Foo that = (Foo) o; return Objects.equals(this.bar, that.bar) && Objects.deepEquals(this.buz, that.buz); } @Override public int hashCode() { return Objects.hash(this.bar, this.buz); } • •

Slide 112

Slide 112 text

// Windows ¥r¥n, Linux ¥n, 昔のMac ¥r String nicerNewLine = System.lineSeparator(); // Windows ¥r¥n, Linux ¥n, 昔のMac ¥r String newLine = System.getProperty("line.separator");

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

Path p1 = Paths.get("path/to/file"); assert !p1.isAbsolute(); Path p2 = Paths.get("/usr/bin/curl"); assert p2.isAbsolute(); Path p3 = Paths.get("path/foo"); Path 相対パス = p1.relativize(p3); /* ../../foo */ assert p1.getParent().endsWith("to"); assert p2.getRoot() != null; assert p1.getNameCount() == 3; assert p1.compareTo(p3) > 0;

Slide 116

Slide 116 text

Path link = Files.createLink(Paths.get("newFile"), Paths.get("existingFile")); List eager = Files.readAllLines(Paths.get("hoge.txt"), Charset.forName("Shift_JIS")); Reader reader = Files.newBufferedReader(Paths.get("hoge.txt"), Charset.forName("EUC-JP"));

Slide 117

Slide 117 text

DirectoryStream stream = Files.newDirectoryStream(Paths.get("画像フォルダ"), new DirectoryStream.Filter() { @Override public boolean accept(final Path entry) throws IOException { return !Files.isDirectory(entry); } }); for (Path 画像 : stream){ // 何か }

Slide 118

Slide 118 text

Path bar = FileSystems.getDefault().getPath("foo/bar"); WatchService watchService = FileSystems.getDefault().newWatchService(); bar.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY, StandardWatchEventKinds.ENTRY_DELETE); while (!Thread.currentThread().isInterrupted()) { System.out.println("監視中..."); WatchKey watchKey = watchService.take(); if (watchKey.isValid()) { for (WatchEvent event : watchKey.pollEvents()) { System.out.printf("イベント発生: %s", event.kind()); Path context = FileSystems.getDefault().getPath("¥¥" + event.context()); process(context, event); } } }

Slide 119

Slide 119 text

• • • •

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

No content

Slide 123

Slide 123 text

1 2 3 4 5 • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • • •

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

import java.awt.event.*; public class MyApp4 extends java.applet.Applet { public void init() { this.addMouseListener(new MyMouseAdapter()); Button approveButton = new Button("承認"); approveButton.addActionListener(new ActionListener() { @Override public void actionPerformed(final ActionEvent evt) { // 承認 // 承認 // 承認 } }); this.add(approveButton); } }

Slide 126

Slide 126 text

Button approveButton = new Button("承認"); approveButton.addActionListener(evt -> { // do something !! });

Slide 127

Slide 127 text

Collections.sort(names, new Comparator() { @Override public int compare(final String a, final String b) { return 0/*ここに比較ロジックが入ります*/; } }); 引数 -> 処理

Slide 128

Slide 128 text

BinaryOperator longer = (String a, String b) -> { return a.length() >= b.length() ? a : b; }; longer = (a, b) -> { return a.length() >= b.length() ? a : b; }; longer = (a, b) -> a.length() >= b.length() ? a : b; supplier = () -> Math.random(); toSet = arg -> Collections.singleton(arg);

Slide 129

Slide 129 text

Set langs = EnumSet.of(JAVA, SCALA, CLOJURE, C_SHARP, F_SHARP, OCAML, RUBY, PYTHON, HASKELL, JAVASCRIPT); List collect = langs.stream() .filter(lang -> lang.isStaticallyTyped()) .map(lang -> lang.displayName()) .sorted() .collect(toList());

Slide 130

Slide 130 text

Set langs = … List collect = langs.stream() .filter(lang -> lang.isStaticallyTyped()) • • interface Collection { default Stream stream() { return …; } }

Slide 131

Slide 131 text

interface Worker { default void work() {…} } interface Person { default void eat() {…} default void play() {…} default void sleep() {…} } class Programmer implements Worker, Person { public void aDayInTheLife() { this.eat(); if (Today.isWeekday()) { this.work(); } else { this.play(); } this.sleep(); } }

Slide 132

Slide 132 text

public String foo(User user) { if (user == null) { return "不明なユーザ"; } else { return user.name(); } } public String bar(Optional user) { return user.map(user_ -> user_.name()) .orElse("不明なユーザ"); } public Optional find(long id) { User user = hoge(id); return Optional.ofNullable(user); }

Slide 133

Slide 133 text

No content

Slide 134

Slide 134 text

• • • • • •

Slide 135

Slide 135 text

No content

Slide 136

Slide 136 text

No content