SlideShare a Scribd company logo
1 of 18
Guavaの食べ方
2013/04/25
松本 陽介
Guavaとは
Javaの共通処理をまとめたライブラリ。
主にGoogleのエンジニアが開発しています。
Apache Commonsがライバル。
Guavaの特徴
 ジェネリクス対応
 並行処理の機能が豊富
 fluent interface
 可読性が高い
 Apache Commonsと併用可
Guavaの機能
 com.google.common.annotations
 com.google.common.base
 com.google.common.cache
 com.google.common.collect
 com.google.common.eventbus
 com.google.common.io
 com.google.common.math
 com.google.common.net
 com.google.common.primitives
 com.google.common.reflect
 com.google.common.util.concurrent
Guavaの機能
 com.google.common.annotations
 com.google.common.base
 com.google.common.cache
 com.google.common.collect
 com.google.common.eventbus
 com.google.common.io
 com.google.common.math
 com.google.common.net
 com.google.common.primitives
 com.google.common.reflect
 com.google.common.util.concurrent
collect
 Lists
List<String> list1 = Lists.newArrayList();
List<String> list2 = Lists.newArrayList(“hoge”, ”fuga”);
final List<String> list3 = ImmutableList.of(“tako”, ”ika”);
collect
 Lists
List<User> userList = getUserList();
// “taro”,”hanako”
List<String> idList = Lists.transform(userList,
new Function<User, String>() {
@Override
public String apply(User user) {
return user.getId();
}
});
collect
 MultiMap
ListMultimap<String, String> map = ArrayListMultiMap.create();
map.put(“pigg”, “life”);
map.put(“pigg”, ”cafe”);
map.put(“card”, ”gf”);
// “life”,”cafe”
List<String> list = map.get(“pigg”);
base
 Joiner
List<String> list = Lists.newArrayList(“hoge”, null, “fuga”);
// “hoge,fuga”
String join = Joiner.on(“,”).skipNulls().join(list);
base
 Splitter
String val = “apple orange,peach”;
CharMatcher matcher = CharMatcher.WHITESPACE
.or(CharMatcher.is(„,‟));
// “apple”,”orange”,”peach”
Iterable<String> split =
Splitter.on(matcher).omitEmptyStrings().split(val);
base
 Preconditions
Integer val = 10;
Preconditions.checkNotNull(val, “required.”);
Preconditions.checkArgument(val > 10, “over 10.”);
base
 Stopwatch
Stopwatch stopwatch = new Stopwatch().start();
execute();
stopwatch.stop();
// “elapsed time:12.3ms”
log.info(“elapsed time:” + stopwatch);
io
 Files
BufferedReader reader = null;
try {
reader = Files.newReader(
new File(“hoge.txt”), Charsets.UTF_8);
} finally {
Closeables.closeQuietly(reader);
}
io
 Files
List<String> lines = Files.readLines(
new File(“fuga.txt”), Charsets.UTF_8);
byte[] bytes = Files.toByteArray(new File(“sample.jpg”));
util.concurrent
 ListeningFuture
ListeningExecutorService threadPool =
MoreExecutors.listeningDecorator(
Executors.newFixedThreadPool(32));
Callable<String> task = getTask();
ListeningFuture<String> future = theadPool.submit(task);
util.concurrent
 ListeningFuture
Futures.addCallback(future, new FutureCallback<String>() {
@Override
public void onSuccess(String s) {
log.info(“success! result:” + s);
}
@Override
public void onFailure(Throwable throwable) {
log.error(“error!”, throwable);
}
});
util.concurrent
 RateLimiter
RateLimiter limiter = RateLimiter.create(100.0);
for (int i = 1; i <= 10000; i++) {
limiter.acquire();
execute();
}
for (byte[] bytes : getBytesList()) {
limiter.acquire(bytes.length);
execute(bytes);
}
まとめ
Guavaを利用するとコーディングしやすくな
り、
ソースの可読性も向上します。便利な機能が沢山
あるので、ぜひ利用してみてください。

More Related Content

Similar to Guavaの美味しい食べ方

Scalaでのプログラム開発
Scalaでのプログラム開発Scalaでのプログラム開発
Scalaでのプログラム開発Kota Mizushima
 
Webサーバ勉強会 LT資料
Webサーバ勉強会 LT資料Webサーバ勉強会 LT資料
Webサーバ勉強会 LT資料学 松崎
 
Web技術勉強会 第31回
Web技術勉強会 第31回Web技術勉強会 第31回
Web技術勉強会 第31回龍一 田中
 
TDC20111031_Groovy_Geb
TDC20111031_Groovy_GebTDC20111031_Groovy_Geb
TDC20111031_Groovy_GebNobuhiro Sue
 
今すぐブラウザでES6を使おう
今すぐブラウザでES6を使おう今すぐブラウザでES6を使おう
今すぐブラウザでES6を使おうHayashi Yuichi
 
Apache Archiva を試す
Apache Archiva を試すApache Archiva を試す
Apache Archiva を試すbouzuya
 
Gws 20120521 gradle
Gws 20120521 gradleGws 20120521 gradle
Gws 20120521 gradleNobuhiro Sue
 
Nseg20120929
Nseg20120929Nseg20120929
Nseg20120929hiro345
 
第1回名古屋Android勉強会Lt用資料
第1回名古屋Android勉強会Lt用資料第1回名古屋Android勉強会Lt用資料
第1回名古屋Android勉強会Lt用資料tantack
 

Similar to Guavaの美味しい食べ方 (13)

Maven基礎
Maven基礎Maven基礎
Maven基礎
 
Scalaでのプログラム開発
Scalaでのプログラム開発Scalaでのプログラム開発
Scalaでのプログラム開発
 
Webサーバ勉強会 LT資料
Webサーバ勉強会 LT資料Webサーバ勉強会 LT資料
Webサーバ勉強会 LT資料
 
Web技術勉強会 第31回
Web技術勉強会 第31回Web技術勉強会 第31回
Web技術勉強会 第31回
 
TDC20111031_Groovy_Geb
TDC20111031_Groovy_GebTDC20111031_Groovy_Geb
TDC20111031_Groovy_Geb
 
今すぐブラウザでES6を使おう
今すぐブラウザでES6を使おう今すぐブラウザでES6を使おう
今すぐブラウザでES6を使おう
 
Apache Archiva を試す
Apache Archiva を試すApache Archiva を試す
Apache Archiva を試す
 
Hadoop 基礎
Hadoop 基礎Hadoop 基礎
Hadoop 基礎
 
Gws 20120521 gradle
Gws 20120521 gradleGws 20120521 gradle
Gws 20120521 gradle
 
Gradle handson
Gradle handsonGradle handson
Gradle handson
 
Golang handson
Golang handsonGolang handson
Golang handson
 
Nseg20120929
Nseg20120929Nseg20120929
Nseg20120929
 
第1回名古屋Android勉強会Lt用資料
第1回名古屋Android勉強会Lt用資料第1回名古屋Android勉強会Lt用資料
第1回名古屋Android勉強会Lt用資料
 

Guavaの美味しい食べ方