Hello, Guava ! samples
Upcoming SlideShare
Loading in...5
×
 

Hello, Guava ! samples

on

  • 1,119 views

Sample codes of the presentation "Hello, Guava !"

Sample codes of the presentation "Hello, Guava !"

Its presentation is here.
http://www.slideshare.net/akirakoyasu/hello-guava

Statistics

Views

Total Views
1,119
Views on SlideShare
1,044
Embed Views
75

Actions

Likes
2
Downloads
12
Comments
0

1 Embed 75

http://www.akirakoyasu.net 75

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial LicenseCC Attribution-NonCommercial License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Hello, Guava ! samples Hello, Guava ! samples Document Transcript

  • Guava-samplesSubject 0. プログラムの実行時間を測りましょう Sample0A(JDK 版) ・・・・・・・・・ Page 1 Sample0B(Guava 版) ・・・・・・・・・ Page 2Subject 1. コストの高い処理の結果をキャッシュしましょう Sample1A(キャッシュ無し版) ・・・・・ Page 3 Sample1B(JDK 版) ・・・・・・・・・ Page 4 Sample1C(Guava 版) ・・・・・・・・・ Page 5Subject 2. あるリストを加工して別のリストを作りましょう Sample2A(JDK 版) ・・・・・・・・・ Page 6 Sample2B(Guava 版) ・・・・・・・・・ Page 7Subject 3. ファイルの内容を標準出力へ出力しましょう Sample3A(JDK 版 旧 io) ・・・・・・・・・ Page 8 Sample3B(JDK 版 nio) ・・・・・・・・・ Page 9 Sample3C(Guava 版) ・・・・・・・・・ Page10 Copyright © 2012 Akira Koyasu Some rights reserved.
  • Sample0A.java 1 p a c k a g e guava.subject0; 2 3 /** 4 * Subject 0. プログラムの実行時間を測りましょう 5 * JDK版 6 * @ a u t h o r koyasu 7 */ 8 p u b l i c c l a s s Sample0A { 910 p u b l i c s t a t i c v o i d main(String[] args) {1112 // システム時間13 l o n g time1 = System. currentTimeMillis ();1415 l o n g sum = 0;16 f o r (i n t i = 0; i < Integer. MAX_VALUE ; i++) { i17 sum += i;18 }1920 l o n g time2 = System. currentTimeMillis ();2122 System. out .printf("sum: %d%n", sum);23 // 引き算をします。24 System. out .printf("time: %d ms%n", time2 - time1);25 }26 }27 Page 1
  • Sample0B.java 1 p a c k a g e guava.subject0; 2 3 i m p o r t com.google.common.base.Stopwatch; 4 5 /** 6 * Subject 0. プログラムの実行時間を測りましょう 7 * Guava版 8 * @ a u t h o r koyasu 9 */10 p u b l i c c l a s s Sample0B {1112 p u b l i c s t a t i c v o i d main(String[] args) {1314 // ストップウォッチ!!15 Stopwatch stopwatch = n e w Stopwatch().start();1617 l o n g sum = 0;18 f o r (i n t i = 0; i < Integer. MAX_VALUE ; i++) { i19 sum += i;20 }2122 stopwatch.stop();2324 System. out .printf("sum: %d%n", sum);25 System. out .printf("time: %s%n", stopwatch);26 }27 }28 Page 2
  • Sample1A.java 1 p a c k a g e guava.subject1; 2 3 i m p o r t java.math.BigInteger; 4 5 i m p o r t com.google.common.base.Stopwatch; 6 7 /** 8 * Subject 1. コストの高い処理の結果をキャッシュしましょう 9 * キャッシュ無し版10 * @ a u t h o r koyasu11 */12 p u b l i c c l a s s Sample1A {1314 p u b l i c s t a t i c v o i d main(String[] args) {1516 Stopwatch stopwatch = n e w Stopwatch().start();1718 BigInteger[] f;19 f = getFibonacci ();20 f = getFibonacci ();21 f = getFibonacci ();2223 System. out .println(f[f.length - 1]);2425 System. out .printf("time: %s%n", stopwatch);26 }2728 /**29 * 20000個のフィボナッチ数を返す30 * @return31 */32 p r i v a t e s t a t i c BigInteger[] getFibonacci() {33 f i n a l i n t n = 20000;34 BigInteger[] f = n e w BigInteger[n];3536 f[0] = BigInteger. ZERO ;37 f[1] = BigInteger. ONE ;38 f o r (i n t i = 0; i < n - 2; i++) { i39 f[i + 2] = f[i].add(f[i + 1]);40 }41 System. out .println("Calc fibonacci complete.");42 r e t u r n f;43 }44 }45 Page 3
  • Sample1B.java 1 p a c k a g e guava.subject1; 2 3 i m p o r t java.math.BigInteger; 4 5 i m p o r t com.google.common.base.Stopwatch; 6 7 /** 8 * Subject 1. コストの高い処理の結果をキャッシュしましょう 9 * JDK版10 * @ a u t h o r koyasu11 */12 p u b l i c c l a s s Sample1B {1314 p u b l i c s t a t i c v o i d main(String[] args) {1516 Stopwatch stopwatch = n e w Stopwatch().start();1718 BigInteger[] f;19 f = FibonacciCalculator. get ();20 f = FibonacciCalculator. get ();21 f = FibonacciCalculator. get ();2223 System. out .println(f[f.length - 1]);2425 System. out .printf("time: %s%n", stopwatch);26 }2728 /**29 * 20000個のフィボナッチ数を返すクラス30 * @return31 */32 p r i v a t e s t a t i c c l a s s FibonacciCalculator {33 p r i v a t e s t a t i c BigInteger[] f ;3435 p r i v a t e s t a t i c BigInteger[] get() {36 // キャッシュを自分で書くのが、良くない37 i f ( f == n u l l ) {38 s y n c h r o n i z e d (FibonacciCalculator.c l a s s ) { c39 i f ( f == n u l l ) {40 f i n a l i n t n = 20000;41 f = n e w BigInteger[n];4243 f [0] = BigInteger. ZERO ;44 f [1] = BigInteger. ONE ;45 f o r (i n t i = 0; i < n - 2; i++) { i46 f [i+2] = f [i].add( f [i+1]);47 }48 System. out .println("Calc fibonacci complete.");49 }50 }51 }52 return f;53 }54 }55 }56 Page 4
  • Sample1C.java 1 p a c k a g e guava.subject1; 2 3 i m p o r t java.math.BigInteger; 4 5 i m p o r t com.google.common.base.Stopwatch; 6 i m p o r t com.google.common.base.Supplier; 7 i m p o r t com.google.common.base.Suppliers; 8 9 /**10 * Subject 1. コストの高い処理の結果をキャッシュしましょう11 * Guava版12 * @ a u t h o r koyasu13 */14 p u b l i c c l a s s Sample1C {1516 p u b l i c s t a t i c v o i d main(String[] args) {1718 Stopwatch stopwatch = n e w Stopwatch().start();1920 BigInteger[] f;21 f = fibonacciSupplier .get();22 f = fibonacciSupplier .get();23 f = fibonacciSupplier .get();2425 System. out .println(f[f.length - 1]);2627 System. out .printf("time: %s%n", stopwatch);28 }2930 // 初回の計算結果をキャッシュするサプライヤー31 p r i v a t e s t a t i c Supplier<BigInteger[]> fibonacciSupplier32 = Suppliers. memoize (n e w Supplier<BigInteger[]>() { n33 p u b l i c BigInteger[] get() {34 f i n a l i n t n = 20000;35 BigInteger[] f = n e w BigInteger[n];3637 f[0] = BigInteger. ZERO ;38 f[1] = BigInteger. ONE ;39 f o r (i n t i = 0; i < n - 2; i++) { i40 f[i+2] = f[i].add(f[i+1]);41 }42 System. out .println("Calc fibonacci complete.");43 r e t u r n f;44 }45 });46 }47 Page 5
  • Sample2A.java 1 p a c k a g e guava.subject2; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 9 i m p o r t com.google.common.base.Stopwatch;1011 /**12 * Subject 2. あるリストを加工して別のリストを作りましょう13 * JDK版14 * @ a u t h o r koyasu15 */16 p u b l i c c l a s s Sample2A {1718 // 元のリスト19 @SuppressWarnings("serial")20 p r i v a t e s t a t i c List<Integer> srcList = n e w ArrayList<Integer>(){{21 f o r (i n t i = 0; i < 1000; i++) { i22 add(Integer. valueOf (i));23 }24 }};2526 // マスタマップ(不変にする)27 @SuppressWarnings("serial")28 p r i v a t e s t a t i c Map<Integer, String> map = Collections. unmodifiableMap (29 n e w HashMap<Integer, String>(){{30 f o r (i n t i = 0; i < 10000; i++) { i31 put(Integer. valueOf (i), "[" + i + "]の値");32 }33 }});3435 p u b l i c s t a t i c v o i d main(String[] args) {36 System. out .printf("src[0]: %s%n", srcList .get(0));37 System. out .printf("map[0]: %s%n", map .entrySet().iterator().next());3839 Stopwatch stopwatch = n e w Stopwatch().start();4041 List<String> dstList = n e w ArrayList<String>( srcList .size());42 // ループして変換する43 f o r (Integer src : srcList ) {44 String val = map .get(src);45 dstList.add(val);46 }4748 stopwatch.stop();4950 System. out .printf("dst[0]: %s%n", dstList.get(0));51 System. out .printf("time: %s%n", stopwatch);52 }53 }54 Page 6
  • Sample2B.java 1 p a c k a g e guava.subject2; 2 3 i m p o r t java.util.ArrayList; 4 i m p o r t java.util.List; 5 i m p o r t java.util.Map; 6 7 import com.google.common.base.Function; 8 import com.google.common.base.Functions; 9 import com.google.common.base.Stopwatch;10 import com.google.common.collect.ImmutableMap;11 import com.google.common.collect.Iterables;12 import com.google.common.collect.Lists;1314 /**15 * Subject 2. あるリストを加工して別のリストを作りましょう16 * Guava版17 * @ a u t h o r koyasu18 */19 p u b l i c c l a s s Sample2B {2021 // 元のリスト22 @SuppressWarnings("serial")23 p r i v a t e s t a t i c List<Integer> srcList = n e w ArrayList<Integer>(){{24 f o r (i n t i = 0; i < 1000; i++) { i25 add(Integer. valueOf (i));26 }27 }};2829 // マスタマップ(不変にする)30 p r i v a t e s t a t i c f i n a l Map<Integer, String> map ;31 static {32 ImmutableMap.Builder<Integer, String> builder33 = ImmutableMap. builder ();34 f o r (i n t i = 0; i < 10000; i++) { i35 builder.put(Integer. valueOf (i), "[" + i + "]の値");36 }37 map = builder.build();38 }3940 p u b l i c s t a t i c v o i d main(String[] args) {41 System. out .printf("src[0]: %s%n", srcList .get(0));42 System. out .printf("map[0]: %s%n", Iterables. get ( map .entrySet(), 0));4344 Stopwatch stopwatch = n e w Stopwatch().start();4546 // 変換関数で変換47 List<String> dstList = Lists. transform (48 srcList , n e w Function<Integer, String>(){49 @Override50 p u b l i c String apply(Integer input) {51 r e t u r n map .get(input);52 }});5354 stopwatch.stop();5556 System. out .printf("dst[0]: %s%n", dstList.get(0));57 System. out .printf("time: %s%n", stopwatch);5859 // 運が良ければこんな方法もある60 List<String> dstList2 = Lists. transform (61 srcList , Functions. forMap ( map ));6263 System. out .println();64 System. out .printf("dst2[0]: %s%n", dstList2.get(0));65 }66 } Page 7
  • Sample3A.java 1 p a c k a g e guava.subject3; 2 3 i m p o r t java.io.BufferedInputStream; 4 i m p o r t java.io.FileInputStream; 5 i m p o r t java.io.IOException; 6 7 i m p o r t com.google.common.base.Stopwatch; 8 9 /**10 * Subject 3. ファイルの内容を標準出力へ出力しましょう11 * JDK版 旧io12 * @ a u t h o r koyasu13 */14 p u b l i c c l a s s Sample3A {1516 p u b l i c s t a t i c v o i d main(String[] args) {17 String path = "/path/to/file.txt";1819 Stopwatch stopwatch = n e w Stopwatch().start();20 BufferedInputStream is = n u l l ;21 try {22 is = n e w BufferedInputStream(n e w FileInputStream(path)); n23 // バッファを用意する24 b y t e [] buf = n e w b y t e [0x1000];25 i n t len;26 // 読み出したバイトを書き込み27 w h i l e ((len = is.read(buf)) >= 0) {28 System. out .write(buf, 0, len);29 }30 } c a t c h (IOException e) {31 e.printStackTrace();32 } finally {33 i f (is != n u l l ) {34 try {35 is.close();36 } c a t c h (IOException e) {37 e.printStackTrace();38 }39 }40 }41 stopwatch.stop();4243 System. out .printf("time: %s%n", stopwatch);44 }45 }46 Page 8
  • Sample3B.java 1 p a c k a g e guava.subject3; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 import java.io.IOException; 6 import java.nio.channels.Channels; 7 import java.nio.channels.FileChannel; 8 9 i m p o r t com.google.common.base.Stopwatch;1011 /**12 * Subject 3. ファイルの内容を標準出力へ出力しましょう13 * JDK版 nio14 * @ a u t h o r koyasu15 */16 p u b l i c c l a s s Sample3B {1718 p u b l i c s t a t i c v o i d main(String[] args) {19 String path = "/path/to/file.txt";2021 Stopwatch stopwatch = n e w Stopwatch().start();2223 FileChannel channel = n u l l ;24 try {25 // ファイルチャネルを使う(悪くない)26 channel = n e w FileInputStream(path).getChannel();27 channel.transferTo(0, channel.size(),28 Channels. newChannel (System. out ));2930 } c a t c h (FileNotFoundException e) {31 e.printStackTrace();32 } c a t c h (IOException e) {33 e.printStackTrace();34 } finally {35 i f (channel != n u l l ) {36 try {37 channel.close();38 } c a t c h (IOException e) {39 e.printStackTrace();40 }41 }42 }434445 stopwatch.stop();4647 System. out .printf("time: %s%n", stopwatch);48 }49 }50 Page 9
  • Sample3C.java 1 p a c k a g e guava.subject3; 2 3 i m p o r t java.io.File; 4 i m p o r t java.io.IOException; 5 6 i m p o r t com.google.common.base.Stopwatch; 7 i m p o r t com.google.common.io.Files; 8 9 /**10 * Subject 3. ファイルの内容を標準出力へ出力しましょう11 * Guava版12 * @ a u t h o r koyasu13 */14 p u b l i c c l a s s Sample3C {1516 p u b l i c s t a t i c v o i d main(String[] args) {17 String path = "/path/to/file.txt";1819 Stopwatch stopwatch = n e w Stopwatch().start();20 try {21 // コピー!!22 Files. copy (n e w File(path), System. out ); n23 } c a t c h (IOException e) {24 e.printStackTrace();25 }26 stopwatch.stop();2728 System. out .printf("time: %s%n", stopwatch);29 }30 }31 Page 10
  • NotesThis work is licensed under the Creative Commons Attribution-NonCommercial 3.0 UnportedLicense. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/.