Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Modern Java

Introduction to Modern Java

社内勉強会資料(2014/06/17 修正)

Takumi Endo

June 15, 2014
Tweet

More Decks by Takumi Endo

Other Decks in Programming

Transcript

  1. ߦ͖ͬͯͨ w  (MBTT'JTI6TFST(SPVQ+BQBOษڧձ w  ++6(φΠτηϛφʮ+BWB&&ಛूʯ w  %FWFMPQFST4VNNJU

    w  &OUFSQSJTFº+BWB8FC"QQMJDBUJPO $POGFSFODF w  ++6(Πϕϯτʮॕˑ+BWB-BVODIʯ w  +BWB%BZ5PLZP
  2. ߦͬͯͳ͍͚Ͳʜ w  +BQBO+#PTT6TFS(SPVQḐ೤ͷ+#PTT w  ++6(φΠτηϛφʮ+BWBղੳπʔϧότ ϧʯ w 

    ++6(+((6(ڞ࠵φΠτηϛφʮ ࣌ؒͰ෼͔Δʂ࣍ੈ୅Ϗϧυπʔϧͷຊ໋(SBEMFͷશ ๴ʯ w  ++6($$$4QSJOH
  3. +BWBϓϥοτϑΥʔϜ छྨ ֓ཁ λʔήοτ +BWB4&ͱͷ ؔΘΓ +BWB4& 4UBOEBSE&EJUJPO ඪ४ͷ ػೳηοτ

    ৭ʑ +BWB&& &OUFSQSJTF&EJUJPO اۀ༻ ػೳηοτ αʔόͳͲ +BWB4&ʴ֦ுػೳ javax.* +BWB.& .JDSP&EJUJPO ૊ΈࠐΈ޲͚ͷ اۀηοτ ૊ΈࠐΈػث +BWB4&ͷ αϒηοτʴ ૊ΈࠐΈ޲͚"1* ˞ ͔ͭͯ͸ͦΕͧΕ+4& +&& +.&ͱݺ͹Ε͍ͯͨ ˞ ʮ+BWB4&&NCFEEFEʯͬͯͷ΋͋Δɻ+BWB.&ͱ͸ผ ˞ +BWB4&ʹ΋javax.*ܥͷ"1*͸͍ͬͺ͍͋Δʢ+/%*ͱ͔ʣ
  4. +43 +BWB4QFDJpDBUJPO3FRVFTU w +BWBքͰͷ3'$ͷΑ͏ͳ΋ͷ w +BWBͷ࢓༷͸+43ͷू·Γ w +BWB4&  +BWB&&

     +BWB.&   w +$1 +BWB$PNNVOJUZ1SPDFTT ͕࢓༷Λࡦఆ w 0SBDMF *#. 3FE)BUͳͲ w -JTUPGBMM+43T IUUQTKDQPSHFOKTSBMM
  5. ϓϩϑΝΠϧ w +BWB&&ʹ͓͍ͯඞཁͳ࢓༷͚ͩબ୒͢Δ࢓૊Έ w 'VMMϓϩϑΝΠϧ w શ෦ೖΓ w 8FCϓϩϑΝΠϧ w

    8FCΞϓϦέʔγϣϯ࢓༷ w IUUQFOXJLJQFEJBPSHXJLJ+BWB@&&@WFSTJPO@IJTUPSZ
  6. ࢀর࣮૷ʢϦϑΝϨϯε࣮૷3*ʣ w ࢓༷Λ࣮ݱ͢Διϑτ΢ΣΞɺϋʔυ΢ΣΞ w +BWB4& 0QFO+%,ͳͲ  w +BWB&& (MBTT'JTIͳͲ

     w +43͸͋͘·Ͱ࢓༷ͳͷͰɺ࣮૷͕ඞཁ w ඪ४࢓༷ͳͷͰʢجຊతʹʣೖΕସ͕͑ޮ͘
  7. +BWB&&ΞϓϦέʔγϣϯαʔό ʢ044ฤʣ ໊લ ࠷৽ όʔδϣϯ +BWB&& උߟ (MBTT'JTI  

    +BWB&& WҎ߱঎༻αϙʔτऴྃ 8JME'MZ چ+#PTT "QQMJDBUJPO4FSWFS 'JOBM  +BWB&& +BWB4&΋ରԠ "QBDIF5PN&&   +BWB&& ݺͼํ͸ʮτϛʔʯ "QBDIF(FSPOJNP   +BWB&&
  8. +BWB&&ΞϓϦέʔγϣϯαʔό ʢ঎༻ϓϩμΫτฤʣ w 8FC-PHJD 0SBDMF  w 8FC4QIFSF *#. 

    w +#PTT&OUFSQSJTF"QQMJDBUJPO1MBUGPSN
  3FE)BU  w ͳͲ
  9. +BWB4&Ͱ΋99͕͍ͨ͠ʂ +43 ࣮ݱྫ +43$POUFYUBOE %FQFOEFODZ*OKFDUJPO GPS+BWB&& $%* 8FMEIUUQXFMEDEJTQFDPSH +43+BWB"1*GPS 8FC4PDLFU

    5ZSVTIUUQTUZSVTKBWBOFU +FUUZIUUQXXXFDMJQTFPSHKFUUZ ˞+FUUZ͸ʙʢ8FC4PDLFUͷରԠࣗମ͸͔Βʣ +43+"9345IF +BWB"1*GPS3&45GVM 8FC4FSWJDFT
 +43+"934 +FSTFZIUUQTKFSTFZKBWBOFU +43+BWB 1FSTJTUFODF +1" )JCFSOBUFIUUQIJCFSOBUFPSH ˞+1"ରԠ͸Ҏ߱ʁ +43#BUDI "QQMJDBUJPOTGPSUIF +BWB1MBUGPSN K#BUDI ࣉా͞Μϒϩά
 IUUQZPTIJPDPNKCBUDIKTSPOKBWBTF؀ڥ
  10. +BWB&&ͷֶͼํ w ϝδϟʔͳ૊Έ߹Θͤ͸(MBTT'JTI /FU#FBOT w ࢲతʹ͸8JME'MZ &DMJQTFʢ͔ͳʁʣ w ࠷ॳ͸&+#ɺ$%*ɺ+"934ɺ+.4ʢɺ+1"ʣ͋ͨΓ w

    νϡʔτϦΞϧ5IF+BWB&&5VUPSJBM w IUUQEPDTPSBDMFDPNKBWBFFUVUPSJBMEPD w +BWB&&ͷࠃ಺ॻ੶͸·ͩͳ͍
  11. +BWBˠ+BWB  w 1SPKFDU$PJO +43  w TXJUDIจͰͷจࣈྻධՁ w ਐ਺Ϧςϥϧɺ਺஋ϦςϥϧͷΞϯμʔόʔ۠੾Γ

    w ྫ֎ͷϚϧνΩϟονͱਖ਼֬ͳ࠶εϩʔ w μΠϠϞϯυԋࢉࢠ w USZXJUISFTPVSDFT w Մม௕Ҿ਺ར༻࣌ͷ@SafeVarargs
  12. ྫ֎ͷϚϧνΩϟον  try { // ... } catch (FooException e)

    { // ... } catch (BarException e) { // ... } catch (BazException e) { // ... } +BWB
  13. Ϧςϥϧ int a = 0b101; // => 5 +BWB ˞0b·ͨ͸0BͰ͸͡ΊΔͱਐ਺Ϧςϥϧ

    int a = 1_000_000; // => 1000000 +BWB ˞ਐ਺ϦςϥϧʹݶΒͣɺ਺஋Ϧςϥϧ͸@Ͱ۠੾Δ͜ͱ͕Ͱ͖Δ
  14. USZXJUISFTPVSDFT  BufferedReader br = null; try { br =

    new BufferedReader(new FileReader(path)); String line = null; while((line = br.readLine()) != null) { // … } } finally { if (br != null) { try { br.close(); } catch(IOException ignore) { } } } +BWB
  15. USZXJUISFTPVSDFT  try (BufferedReader br = new BufferedReader(new FileReader(path)) {

    String line = null; while((line = br.readLine()) != null) { // … } } +BWB ˞ ϦιʔεΛ࣮֬ʹΫϩʔζͯ͘͠ΕΔ ˞ Ϧιʔε͸ෳ਺ఆٛՄʢ࡞੒ͷٯॱʹΫϩʔζ͞ΕΔʣ ˞ tryϒϩοΫͱtry-with-resourcesͷ྆ํ͔Βྫ֎͕εϩʔ͞Ε ͨ৔߹ɺޙऀͷྫ֎͸཈੍͞ΕΔʢ#getSuppressedͰऔಘՄʣ ˞ catchϒϩοΫ΍finallyϒϩοΫ͸Ϋϩʔζޙʹ࣮ߦ͞ΕΔ
  16. /*0 BufferedReader br = Files.newBufferedReader( Paths.get("a.txt"), StandardCharsets.UTF_8); +BWB ˞ ҆શͳจࣈίʔυࢦఆ

    ˞ ศརͳϢʔςΟϦςΟ͕͍ͬͺ͍͋Δ List<String> lines = Files.readAllLines( Paths.get("a.txt"), StandardCharsets.UTF_8); +BWB
  17. +BWBˠ+BWB  w ϓϩάϥϜతͳมߋϙΠϯτʢҰ෦ʣ w ϥϜμࣜ +43 ɺ4USFBN"1* w %BUFBOE5JNF"1*

    +43  w 0QUJPOBM w ූ߸ͳ͠੔਺ w ΠϯλϑΣʔεͷσϑΥϧτ࣮૷ w λΠϓΞϊςʔγϣϯ
  18. ϥϜμࣜʙجຊʙ list.sort(new Comparator<String>() { @Override public int compare(String s1, String

    s2) { return s1.length() - s2.length(); } }); +BWB list.sort((String s1, String s2) -> { return s1.length() - s2.length(); }); +BWB
  19. ϥϜμࣜʙলུʙ list.sort((s1, s2) -> s1.length() - s2.length()); list.sort((s1, s2) ->

    { return s1.length() - s2.length(); }); +BWB ˞ ࣗ໌ͳҾ਺ͷܕ͸লུͰ͖Δ +BWB ˞ {}Ͱғ·ͳ͍৔߹ɺࣜͷ݁Ռ͕ͦͷ··ϥϜμࣜͷ໭Γ஋ʹͳΔ
  20. 4USFBN"1*ಋೖฤ for (String s : list) { System.out.println(s); } +BWB

    list.forEach((s) -> System.out.println(s)); +BWB +BWB ϝιουࢀরͰ͞Βʹ؆ܿʹ list.forEach(System.out::println);
  21. ετϦʔϜɾύΠϓϥΠϯ List<Integer> list = Arrays.asList(5, 2, 4, 3, 1); !

    List<Integer> list2 = list.stream() .filter(n -> n >= 3) .map(n -> n *= 2) .sorted((n1, n2) -> n1 - n2) .collect(Collectors.toList()); +BWB ^தؒૢ࡞ ऴ୺ૢ࡞ ˞ ݸҎ্ͷதؒૢ࡞ͱ࠷ޙͷͭͷऴ୺ૢ࡞͔ΒͳΔ ˞ தؒૢ࡞͸஗Ԇ͞Εɺऴ୺ૢ࡞࣌ʹ·ͱΊ࣮ͯߦ͞ΕΔ ˞ stream()ͷ͔ΘΓʹparallelStream()Λ࢖͏ͱฒྻॲཧͰ͖Δ ετϦʔϜੜ੒
  22. ࣮࣭తpOBM String msg = "test"; ! Runnable r1 = new

    Runnable() { @Override public void run() { System.out.println(msg); } }; ! // ϥϜμ Runnable r2 = () -> { System.out.println(msg); }; +BWB +BWB final String msg = "test"; ! Runnable r1 = new Runnable() { @Override public void run() { System.out.println(msg); } }; ˞ finalͳ͠ͰࢀরՄೳʢ࠶୅ೖ͢ΔͱίϯύΠϧΤϥʔʹͳΔʣ
  23. ϙʔλϒϧ8FCΞϓϦέʔγϣϯ w +BWB&&ͱผͷΞϓϩʔνͱͯ͠ɺKBSXBSҰͭͰಈ͔ͤΔΑ͏ ͳܗଶͰͷΞϓϦέʔγϣϯ w +FOLJOT IUUQKFOLJOTDJPSH  w (JU#VDLFU

    IUUQTHJUIVCDPNUBLF[PFHJUCVDLFU  w ͦͷͨΊͷϑϨʔϜϫʔΫ w %SPQXJ[BSE IUUQTESPQXJ[BSEHJUIVCJPESPQXJ[BSE  w /JOKB IUUQOJOKBGSBNFXPSLPSH
  24. Ϟμϯͳ+BWBςετ w ୯ମςετ w +6OJUʢ&ODMPTFEɺ5IFPSZ͋ͨΓͷൺֱత৽͍͠ػೳʣ w (SPPWZͰॻ͘ w ݁߹ςετ w

    "SRVJMMJBO w ෛՙςετ w (BUMJOH w ϒϥ΢βςετ w (FC 4QPDL w (SBQIFOFʢ"SRVJMMJBOͱͷ౷߹ʣ