Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.
make your code easier and faster
New data structures 
● Multimap: one key - several values 
○ HashMultimap - does not allow duplicating key-value pairs 
○ ...
Multimap before 
1. Map<Employee, List<Role>> map = new HashMap<Employee, List<Role>>(); 
2. public void grant(Employee em...
Multimap after 
1. Multimap<Employee, Role> multimap = ArrayListMultimap.create(); 
2. public void grant(Employee employee...
BiMap 
● Bidirectional map - values can be used as keys. 
1. BiMap<String, Integer> bimap = HashBiMap.create(); 
2. bimap....
Multiset 
● allows adding duplicates 
● stores the number of duplicates 
● HashMultiset, LinkedHashMultiset, TreeMultiset,...
Immutable collections 
● ImmutableList, ImmutableSet, ImmutableMap etc. 
ImmutableSet<Integer> LUCKY_NUMBERS = 
ImmutableS...
ImmutableMap builder 
1. Map<Integer, String> numberNamesExtended = 
2. ImmutableMap.<Integer, String>builder()() 
3. .put...
Utils 
● Lists, Sets, Map, Strings 
● Iterables, FluentIterable, Optional 
● Predicate, Function 
● Ordering, Range 
Lets ...
Articles 
● http://docs.guava-libraries.googlecode. 
com/git/javadoc/index.html?overview-summary.html 
● http://java.dzone...
Nächste SlideShare
Wird geladen in …5
×

Google Guava

464 Aufrufe

Veröffentlicht am

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

Veröffentlicht in: Technologie
  • Als Erste(r) kommentieren

  • Gehören Sie zu den Ersten, denen das gefällt!

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

×