Google collections api an introduction


Published on

Google Collections API

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Make sure every machine has JBoss installed
  • Make sure everyone has this done on their local JBoss install
  • Google collections api an introduction

    1. 1. By Jitendra Gosain Google Collections API
    2. 2. Introduction <ul><li>What is Google Collections API? </li></ul><ul><li>Its a utility API. </li></ul><ul><li>Its an extension to the Java Collections API. </li></ul><ul><li>Its a set of new Collection Types and implementations. </li></ul><ul><li>Part of the Project called “ Guava ” </li></ul><ul><li>Requires “ JDK 1.5” </li></ul>
    3. 3. Key Interfaces and Classes
    4. 4. Multimap <ul><li>A collection similar to a Map, but which may associate multiple values with a single key. </li></ul><ul><li>Keys and values can be null. </li></ul><ul><li>Adding a new key-value pair equal to an existing key-value pair has no effect. </li></ul>
    5. 5. HashMultimap Example <ul><li>HashMultimap<Integer, String> hm = HashMultimap.create (); </li></ul><ul><li>hm.put(1, &quot;first&quot;);//Add Values to keys </li></ul><ul><li>hm.put(2, &quot;second&quot;); </li></ul><ul><li>hm.put(1, &quot;third&quot;); //Same key is added a new value </li></ul><ul><li>hm.put(1, &quot;fourth&quot;); </li></ul><ul><li>//hm is {1=[fourth, third, first], 2=[second]} </li></ul>
    6. 6. HashMultimap Example <ul><li>hm.remove(1,&quot;first&quot;);//{1=[fourth, third], 2=[second]} </li></ul><ul><li>hm.removeAll(1);//{2=[second]} </li></ul><ul><li>Multimap safeMultimap = Multimaps. synchronizedMultimap (hm); </li></ul><ul><li>SetMultimap safeSetMultimap = Multimaps. synchronizedSetMultimap (hm); </li></ul><ul><li>Map mp = hm. asMap (); //Converts to Java Map </li></ul>
    7. 7. Multiset <ul><li>A Multiset can have duplicates unlike a Java Set. </li></ul><ul><li>Also called a “ Bag ”. </li></ul><ul><li>The total number of occurrences of an element in a multiset is called the  count  of that element. </li></ul>
    8. 8. HashMultiset Example <ul><li>HashMultiset ms = HashMultiset. create (); </li></ul><ul><li>ms.add(&quot;1&quot;); </li></ul><ul><li>ms.add(&quot;2&quot;); </li></ul><ul><li>ms.add(&quot;1&quot;); </li></ul><ul><li>ms.add(&quot;3&quot;, 3); </li></ul><ul><li>//HashMultiset ms is [3 x 3, 2, 1 x 2] </li></ul>
    9. 9. HashMultiset <ul><li>Set emSet = ms. elementSet (); </li></ul><ul><li>//[3, 2, 1] – Above returns set of distinct elements </li></ul><ul><li>  </li></ul><ul><li>ms.remove(&quot;1&quot;, 1); //Set is now [3 x 3, 2, 1] </li></ul><ul><li>ms.remove(&quot;1&quot;, ms.count(&quot;1&quot;)); //[3 x 3, 2] </li></ul><ul><li>Set set = new HashSet(); </li></ul><ul><li>set.add(&quot;2&quot;); </li></ul><ul><li>ms. retainAll (set); </li></ul><ul><li>//[2] – Retains the elements of Collection “set” </li></ul>
    10. 10. BiMap <ul><li>A bimap (or &quot; bidirectional map &quot;) is a map that preserves the uniqueness of its values as well as that of its keys and looking up a key from a value is possible . </li></ul><ul><li>Also called “ unique-valued map ”. </li></ul><ul><li>A HashBiMap and its inverse are both serializable. </li></ul>
    11. 11. BiMap Example <ul><li>HashBiMap<Integer, String> hmBiMap = </li></ul><ul><li>HashBiMap. create (); </li></ul><ul><li>hmBiMap.put(1, &quot;first&quot;); </li></ul><ul><li>hmBiMap.put(2, &quot;second&quot;); </li></ul><ul><li>hmBiMap.put(2, “newsecond&quot;); //2 holds this </li></ul><ul><li>hmBiMap.put(3, &quot;second&quot;); </li></ul><ul><li>//Above throws an error (value already present) </li></ul>
    12. 12. BiMap Example <ul><li>hmBiMap. inverse (); </li></ul><ul><li>//Above returns the inverse view of this bimap, value mapped to key now. </li></ul><ul><li>Set set = hmBiMap. values (); </li></ul><ul><li>//Returns a collection of values of the bimap </li></ul>
    13. 13. ImmutableMap <ul><li>An instance of ImmutableMap contains its own data that will never change . </li></ul><ul><li>Provides read-only access of data. </li></ul><ul><li>The Java “Collections.unmodifiableMap” which is a  view  of a separate map which can still change, an instance of ImmutableMap contains its own data and will  never  change. </li></ul><ul><li>Also called the “ Constant Maps ”. </li></ul>
    14. 14. ImmutableMap Example <ul><li>ImmutableMap<String,Integer> map1 = </li></ul><ul><li> new ImmutableMap. Builder <String,Integer>() </li></ul><ul><li> .put(&quot;one&quot;, 1) </li></ul><ul><li> .put(&quot;two&quot;, 2) </li></ul><ul><li> .put(&quot;three&quot;, 3) </li></ul><ul><li> . build ();//Build an Immutable map </li></ul>
    15. 15. ImmutableMap Example <ul><li>map1.put(&quot;four&quot;, 4); //Not allowed </li></ul><ul><li>map1.remove(4); //Not allowed </li></ul><ul><li>//Above throws “UnsupportedOperationException” </li></ul><ul><li>map1. containsKey (&quot;one&quot;); //true </li></ul><ul><li>map1. containsValue (3); //true </li></ul>
    16. 16. ImmutableMap Example <ul><li>Map<String, String> javaMap = new HashMap<String, String>(); </li></ul><ul><li>javaMap.put(&quot;key1&quot;, &quot;value1&quot;); </li></ul><ul><li>javaMap.put(&quot;key2&quot;, &quot;value2&quot;); </li></ul><ul><li>ImmutableMap<String,String> map = ImmutableMap.of (&quot;key1&quot;, &quot;value1&quot;, &quot;key2&quot;, &quot;value2&quot;); </li></ul>
    17. 17. ImmutableSet <ul><li>An instance of the ImmutableSet contains its own data that will not change . </li></ul><ul><li>ImmutableSet set1 = new ImmutableSet. Builder ().add(&quot;1&quot;).add(&quot;2&quot;) .build(); </li></ul><ul><li>set1.add(“3”); //Its not Allowed </li></ul><ul><li>set1.remove(“1”); //Not Allowed </li></ul><ul><li>ImmutableSet set2 = ImmutableSet.of (&quot;1&quot;, &quot;2&quot;, &quot;3&quot;); </li></ul><ul><li> </li></ul>
    18. 18. ImmutableList <ul><li>An instance of ImmutableList contains its own data that will never change . </li></ul><ul><li>List<String> myList = new ArrayList<String>(); </li></ul><ul><li>myList.add(&quot;first&quot;); </li></ul><ul><li>myList.add(&quot;second&quot;); </li></ul>
    19. 19. ImmutableList Example <ul><li>List<String> readOnlyList = Collections.unmodifiableList (myList); </li></ul><ul><li>readOnlyList.add(&quot;fourth&quot;); //Will throw Exception </li></ul><ul><li>ImmutableList list1 = new ImmutableList. Builder ().add(&quot;1&quot;).add(&quot;2&quot;).build(); </li></ul><ul><li>list1.add(&quot;5&quot;); //Not Allowed </li></ul><ul><li>List.remove(“1”); //Not Allowed </li></ul>
    20. 20. ImmutableList Example <ul><li>List<String> constList = new ArrayList<String>(); </li></ul><ul><li>constList.add(&quot;a&quot;); </li></ul><ul><li>constList.add(&quot;b&quot;); </li></ul><ul><li>ImmutableList<String> immutableList = ImmutableList.of (&quot;a&quot;, &quot;b&quot;); </li></ul>
    21. 21. MapDifference <ul><li>An interface representing the differences between the two given maps. </li></ul><ul><li>ImmutableMap<String,Integer> map1 = </li></ul><ul><li> new ImmutableMap.Builder <String,Integer>() </li></ul><ul><li>.put(&quot;one&quot;, 1).put(&quot;two&quot;, 2).put(&quot;three&quot;, 3).build(); </li></ul><ul><li>//Above creates an ImmutableMap </li></ul><ul><li>// map1 is now {one=1, two=2, three=3} </li></ul>
    22. 22. MapDifference <ul><li>ImmutableMap<String,Integer> map2 = </li></ul><ul><li>new ImmutableMap.Builder <String,Integer>() </li></ul><ul><li>.put(&quot;five&quot;, 5) </li></ul><ul><li>.put(&quot;four&quot;, 4) </li></ul><ul><li>.put(&quot;three&quot;, 3) </li></ul><ul><li>.put(&quot;one&quot;, 10) </li></ul><ul><li>.build(); </li></ul><ul><li>// map2 is {five=5, four=4, three=3, one=10} </li></ul>
    23. 23. MapDifference <ul><li>MapDifference <String, Integer> difference = Maps.difference (map1, map2); </li></ul><ul><li>difference. entriesInCommon (); </li></ul><ul><li>difference. entriesDiffering (); </li></ul><ul><li>difference. entriesOnlyOnLeft (); </li></ul><ul><li>difference. entriesOnlyOnRight (); </li></ul>
    24. 24. MapDifference <ul><li>difference. areEqual (); </li></ul><ul><li>MapDifference.ValueDifference <Integer> val = (MapDifference.ValueDifference<Integer>) difference.entriesDiffering(). get (&quot;one&quot;); </li></ul>
    25. 25. Preconditions <ul><li>A final class ( which provides static methods to verify correct arguments and state. </li></ul><ul><li>String str = null; </li></ul><ul><li>Preconditions. checkNotNull( str); </li></ul><ul><li>int value = -1; </li></ul><ul><li>Preconditions. checkArgument (value >= 0, &quot;negative value&quot;); </li></ul>
    26. 26. Function Interface <ul><li>A Function ( is used in transformation of one object to the another. </li></ul><ul><li>Represented as public interface Function<F,T> </li></ul><ul><li>First argument is the “function input” and second argument is the “function output”. </li></ul>
    27. 27. Function Interface Example <ul><li>Function<String, String> extractUserName = new Function<String, String>(){ </li></ul><ul><li>public String apply (String emailAddress) { </li></ul><ul><ul><li>String userName = emailAddress.substring(0, emailAddress.indexOf('@')); </li></ul></ul><ul><ul><li>return userName; </li></ul></ul><ul><li>} </li></ul><ul><li>}; </li></ul>
    28. 28. Function Example <ul><li>List<String> emailList = Lists.newArrayList(&quot;;, &quot;;, &quot;;); </li></ul><ul><li>List<String> userNameList = Lists. transform (emailList, extractUserName ); </li></ul>
    29. 29. Function Example <ul><li>Function<String, Integer> functionStringToInteger = new Function<String, Integer>(){ </li></ul><ul><li> public Integer apply (String str){ </li></ul><ul><li> return Integer.parseInt(str); </li></ul><ul><li> } </li></ul><ul><li> }; </li></ul><ul><li>List strTemp = Lists.newArrayList(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;4&quot;); </li></ul><ul><li>List intList = Lists.transform(strTemp, functionStringToInteger ); </li></ul>
    30. 30. Functions Class <ul><li>Function toStringFunction = Functions.toStringFunction (); </li></ul><ul><li>List intList = Lists.newArrayList(1,2,3,4,5); </li></ul><ul><li>Lists.transform( intList, toStringFunction ); </li></ul>
    31. 31. Predicate <ul><li>A predicate is a way to specify some conditions based on an instance of a class </li></ul><ul><li>The defined predicate then can be used to selectively filter out matching instances of that class from a collection. </li></ul><ul><li>Predicate<Integer> salaryCut = new Predicate <Integer>() { </li></ul><ul><li> public boolean apply (Integer salary) { </li></ul><ul><li> return salary > 50000; </li></ul><ul><li> } </li></ul><ul><li>}; </li></ul>
    32. 32. Predicate Example <ul><li>List<Integer> salaryList = Lists.newArrayList(); </li></ul><ul><li>salaryList.add(new Integer(20000)); </li></ul><ul><li>salaryList.add(new Integer(60000)); </li></ul><ul><li>salaryList.add(new Integer(70000)); </li></ul><ul><li>salaryList.add(new Integer(40000)); </li></ul><ul><li>List<Integer> filteredSalary = Lists.newArrayList( Iterables.filter (salaryList, salaryCut )); </li></ul>
    33. 33. Predicate Example <ul><li>List<String> list1 = Lists.newArrayList(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;); </li></ul><ul><li>List<String> list2 = Lists.newArrayList(&quot;1&quot;, &quot;4&quot;, &quot;5&quot;); </li></ul><ul><li>List<String> list3 = Lists.newArrayList(&quot;1&quot;, &quot;4&quot;, &quot;6&quot;); </li></ul><ul><li>boolean result = and ( in (list1),in(list2), in(list3)).apply(&quot;4&quot;); </li></ul><ul><li>result = or (in(list1),in(list2), in(list3)).apply(&quot;4&quot;); </li></ul>
    34. 34. Predicates <ul><li>ImmutableMap<Integer,String> map = new ImmutableMap.Builder<Integer,String>() </li></ul><ul><li>.put(10, &quot;Ten&quot;) </li></ul><ul><li>.put(20, &quot;Twenty&quot;) </li></ul><ul><li>.put(30, &quot;Thirty&quot;) .build(); </li></ul><ul><li>Map<Integer,String> filtered11 = Maps.filterKeys (map,Predicates. or ( Predicates. equalTo (10), Predicates. equalTo (30))); </li></ul>
    35. 35. Miscellaneous <ul><li>Joiner ( </li></ul><ul><li>ImmutableSet s1 = ImmutableSet.of(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;); </li></ul><ul><li>String joinedStr = Joiner.on (&quot;:&quot;). join (s1) ; </li></ul><ul><li>Joiner joiner = Joiner.on(&quot;* &quot;).skipNulls(); </li></ul><ul><li>String strWithStar = joiner.join(“A&quot;, null, “B&quot;, “C&quot;); </li></ul>
    36. 36. Joiner <ul><li>joiner = Joiner.on(&quot;* &quot;). useForNull (&quot;Default&quot;); </li></ul><ul><li>String str = joiner. join (“A&quot;, null, “B&quot;, “C&quot;); </li></ul><ul><li>Map<String, String> mp = new LinkedHashMap<String, String>(); </li></ul><ul><li>mp.put(&quot;1&quot;, &quot;1&quot;); mp.put(&quot;2&quot;, &quot;2&quot;); </li></ul><ul><li>String str = Joiner.on </li></ul><ul><li>(&quot;, &quot;). withKeyValueSeparator (&quot;=&quot;).join(mp); </li></ul><ul><li>//1=1, 2=2 </li></ul>
    37. 37. Collections2 <ul><li>Collections2 </li></ul><ul><li>Predicate<Integer> salaryCut = new Predicate <Integer>() { </li></ul><ul><li> public boolean apply (Integer salary) { </li></ul><ul><li> return salary > 50000; </li></ul><ul><li> } </li></ul><ul><li>}; </li></ul>
    38. 38. Collections2 <ul><li>List<Integer> salaryList = Lists. newArrayList(); </li></ul><ul><li>salaryList.add(new Integer(20000)); </li></ul><ul><li>salaryList.add(new Integer(60000)); </li></ul><ul><li>salaryList.add(new Integer(70000)); </li></ul><ul><li>salaryList.add(new Integer(40000)); </li></ul><ul><li>Collection<Integer> newSalaryList = Collections2.filter (salaryList, salaryCut); </li></ul><ul><li>newSalaryList.add(1000);//Exception </li></ul><ul><li>salaryList. clear ();//newSalaryList gets empty too </li></ul>
    39. 39. Maps <ul><li>Static Utility API’s for Java Map. </li></ul><ul><li>Map<Integer, String> tmpMap = new HashMap<Integer, String> (); </li></ul><ul><li>Map<Integer, String> tmpMap1 = Maps.newHashMap (); </li></ul><ul><li>//{10=Ten, 20=Twenty, 30=Thirty} </li></ul><ul><li>Maps. filterKeys (map, Predicates.equalTo(10)); </li></ul><ul><li>Maps. filterValues (map, Predicates.equalTo(&quot;Ten&quot;)); </li></ul>
    40. 40. <ul><li>Map<Integer,String> transformed = Maps.transformValues (map, new Function<String,String>() { </li></ul><ul><li>public String apply(String from) { </li></ul><ul><li>return &quot;A &quot; + from; </li></ul><ul><li>} </li></ul><ul><li>}); </li></ul><ul><li>//10=A Ten, 20=A Twenty, 30=A Thirty} </li></ul>
    41. 41. Lists <ul><li>List<Integer> listA = Lists.newArrayList (); </li></ul><ul><li>listA.add(new Integer(1)); </li></ul><ul><li>listA.add(new Integer(2)); </li></ul><ul><li>listA.add(new Integer(3)); </li></ul><ul><li>listA.add(new Integer(4)); </li></ul><ul><li>listA.add(new Integer(5)); </li></ul><ul><li>List finalLists = Lists.partition (listA, 3); </li></ul>
    42. 42. Sets <ul><li>HashSet<String> hSet = Sets.newHashSet() ; </li></ul><ul><li>ImmutableSet<String> set1 = ImmutableSet.of(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;); </li></ul><ul><li>ImmutableSet<String> set2 = ImmutableSet.of(&quot;2&quot;, &quot;3&quot;, &quot;4&quot;); </li></ul><ul><li>Sets.union (set1, set2); </li></ul><ul><li>Sets.intersection (set1, set2); </li></ul><ul><li>Sets.difference (set1, set2); </li></ul><ul><li>Sets.filter (set1, Predicates.equalTo(&quot;1&quot;)) </li></ul>
    43. 43. Where can I refer the API’s? Reference: http:// -collections/
    44. 44. <ul><li>Thank You! </li></ul>