Guavaの美味しい食べ方

471 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
471
On SlideShare
0
From Embeds
0
Number of Embeds
39
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Guavaの美味しい食べ方

  1. 1. Guavaの食べ方2013/04/25松本 陽介
  2. 2. GuavaとはJavaの共通処理をまとめたライブラリ。主にGoogleのエンジニアが開発しています。Apache Commonsがライバル。
  3. 3. Guavaの特徴 ジェネリクス対応 並行処理の機能が豊富 fluent interface 可読性が高い Apache Commonsと併用可
  4. 4. 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
  5. 5. 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
  6. 6. collect ListsList<String> list1 = Lists.newArrayList();List<String> list2 = Lists.newArrayList(“hoge”, ”fuga”);final List<String> list3 = ImmutableList.of(“tako”, ”ika”);
  7. 7. collect ListsList<User> userList = getUserList();// “taro”,”hanako”List<String> idList = Lists.transform(userList,new Function<User, String>() {@Overridepublic String apply(User user) {return user.getId();}});
  8. 8. collect MultiMapListMultimap<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”);
  9. 9. base JoinerList<String> list = Lists.newArrayList(“hoge”, null, “fuga”);// “hoge,fuga”String join = Joiner.on(“,”).skipNulls().join(list);
  10. 10. base SplitterString val = “apple orange,peach”;CharMatcher matcher = CharMatcher.WHITESPACE.or(CharMatcher.is(„,‟));// “apple”,”orange”,”peach”Iterable<String> split =Splitter.on(matcher).omitEmptyStrings().split(val);
  11. 11. base PreconditionsInteger val = 10;Preconditions.checkNotNull(val, “required.”);Preconditions.checkArgument(val > 10, “over 10.”);
  12. 12. base StopwatchStopwatch stopwatch = new Stopwatch().start();execute();stopwatch.stop();// “elapsed time:12.3ms”log.info(“elapsed time:” + stopwatch);
  13. 13. io FilesBufferedReader reader = null;try {reader = Files.newReader(new File(“hoge.txt”), Charsets.UTF_8);} finally {Closeables.closeQuietly(reader);}
  14. 14. io FilesList<String> lines = Files.readLines(new File(“fuga.txt”), Charsets.UTF_8);byte[] bytes = Files.toByteArray(new File(“sample.jpg”));
  15. 15. util.concurrent ListeningFutureListeningExecutorService threadPool =MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(32));Callable<String> task = getTask();ListeningFuture<String> future = theadPool.submit(task);
  16. 16. util.concurrent ListeningFutureFutures.addCallback(future, new FutureCallback<String>() {@Overridepublic void onSuccess(String s) {log.info(“success! result:” + s);}@Overridepublic void onFailure(Throwable throwable) {log.error(“error!”, throwable);}});
  17. 17. util.concurrent RateLimiterRateLimiter 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);}
  18. 18. まとめGuavaを利用するとコーディングしやすくなり、ソースの可読性も向上します。便利な機能が沢山あるので、ぜひ利用してみてください。

×