• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Google collections api   an introduction
 

Google collections api an introduction

on

  • 4,676 views

Google Collections API

Google Collections API

Statistics

Views

Total Views
4,676
Views on SlideShare
4,676
Embed Views
0

Actions

Likes
1
Downloads
79
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Make sure every machine has JBoss installed
  • Make sure everyone has this done on their local JBoss install

Google collections api   an introduction Google collections api an introduction Presentation Transcript

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