Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Google Guava

462 views

Published on

Oleh Dovhai, Java developer, tells about Guava features and shows how it works.
Presentation includes:
-basic overview of Guava collections and utils;
-some practical examples;
-comparison of code and execution time with other utils.

Exampler can be found here:
http://goo.gl/x4XJfi

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Google Guava

  1. 1. make your code easier and faster
  2. 2. New data structures ● Multimap: one key - several values ○ HashMultimap - does not allow duplicating key-value pairs ○ LinkedListMultimap - traversal in order of adding ○ TreeMultimap - sorted Multimap
  3. 3. Multimap before 1. Map<Employee, List<Role>> map = new HashMap<Employee, List<Role>>(); 2. public void grant(Employee employee, Role role) { 3. List<Role> roles = map.get(employee); 4. if (roles == null) { 5. roles = new ArrayList<Role>(); 6. map.put(employee, roles); 7. } 8. roles.add(role); 9. }
  4. 4. Multimap after 1. Multimap<Employee, Role> multimap = ArrayListMultimap.create(); 2. public void grant(Employee employee, Role role) { 3. multimap.put(employee, role); 4. }
  5. 5. BiMap ● Bidirectional map - values can be used as keys. 1. BiMap<String, Integer> bimap = HashBiMap.create(); 2. bimap.put("ONE", 1); 3. bimap.put("TWO", 2); 4. BiMap<Integer, String> inversed = bimap.inverse();
  6. 6. Multiset ● allows adding duplicates ● stores the number of duplicates ● HashMultiset, LinkedHashMultiset, TreeMultiset, ConcurrentHashMultiset 1. Multiset<String> tags = HashMultiset.create(); 2. for (BlogPost post : getAllBlogPosts()) { 3. tags.addAll(post.getTags()); 4. } 5. tags.elementSet(); 6. tags.count("habr");
  7. 7. Immutable collections ● ImmutableList, ImmutableSet, ImmutableMap etc. ImmutableSet<Integer> LUCKY_NUMBERS = ImmutableSet.of(4, 8, 15, 16, 23, 42); ImmutableMap<Integer, String> numberNames = ImmutableMap.of(1, "ONE", 2, "TWO", 3, "THREE"); //max 5 elements by .of
  8. 8. ImmutableMap builder 1. Map<Integer, String> numberNamesExtended = 2. ImmutableMap.<Integer, String>builder()() 3. .putAll(numberNames) 4. .put(5, "FIVE") 5. .put(6, "SIX") 6. .build();
  9. 9. Utils ● Lists, Sets, Map, Strings ● Iterables, FluentIterable, Optional ● Predicate, Function ● Ordering, Range Lets practice
  10. 10. Articles ● http://docs.guava-libraries.googlecode. com/git/javadoc/index.html?overview-summary.html ● http://java.dzone.com/articles/google-guava-cleaner-code ● http://habrahabr.ru/post/91215

×