Guava ClassToInstanceMap Example

Guava ClassToInstanceMap

A special type of collection that uses the class as the key and the instance of that class as the value. In this example, my ClassToInstanceMap allows me to store all sorts of different types of wrapper classes in myNumberMap collection. These all happen to be numeric types (wrapper classes) for Java primitive types and store the values of the types in the collection. In addition, I have created another collection called myDataMap which is storing classes like Customer and Associate although doing so was not a necessity.

Methods available to ClassToInstanceMap

@GwtCompatible
public interface ClassToInstanceMap<B>
extends Map<Class<? extends B>,B>

#Method and Description
1static <B> MutableClassToInstanceMap<B> create()
Returns a new MutableClassToInstanceMap instance backed by a HashMap using the default initial capacity and load factor.
2static <B> MutableClassToInstanceMap<B> create(Map<Class<? extends B>,B> backingMap)
Returns a new MutableClassToInstanceMap instance backed by a given empty backingMap.
3protected Map<K,V> delegate()
Returns the backing delegate instance that methods are forwarded to.
4Set<Map.Entry<K,V>> entrySet()
Returns a Set view of the mappings contained in this map.
5<T extends B> T getInstance(Class<T> type)
Returns the value the specified class is mapped to, or null if no entry for this class is present.
6V put(K key, V value)
Associates the specified value with the specified key in this map (optional operation).
7void putAll(Map<? extends K,? extends V> map)
Copies all of the mappings from the specified map to this map (optional operation).
8<T extends B> T putInstance(Class<T> type, T value)
Maps the specified class to the specified value.

ClassToInstanceMap Implementations

There are two implementation classes available for ClassToInstanceMap:

  • ImmutableClassToInstanceMap
  • MutableClassToInstanceMap

Java Example using Guava ClassToInstanceMap

package com.avaldes;

import com.avaldes.model.Associate;
import com.avaldes.model.Customer;
import com.google.common.collect.ClassToInstanceMap;
import com.google.common.collect.MutableClassToInstanceMap;

public class GuavaClassToInstanceMapExample {
  private static ClassToInstanceMap<Number> myNumberMap = MutableClassToInstanceMap.create();
  private static ClassToInstanceMap<Object> myDataMap = MutableClassToInstanceMap.create();

  public static void main(String[] args) {
    myNumberMap.putInstance(Integer.class, 1276);
    myNumberMap.putInstance(Double.class, 11.751897298);
    myNumberMap.putInstance(Float.class, 5.125f);
    myNumberMap.putInstance(Long.class, 1982092L);
    
    System.out.println("\n---[MutableClassToInstanceMap with Number]-----");
    System.out.println("Value for Long.class is: " + myNumberMap.get(Long.class));
    System.out.println("Value for Float.class is: " + myNumberMap.get(Float.class));
    System.out.println("Value for Integer.class is: " + myNumberMap.get(Integer.class));
    System.out.println("Value for Double.class is: " + myNumberMap.get(Double.class));

    Associate associate = new Associate("111", "Amaury", "Valdes", "IT", "200", "IBM Global Solutions", "E");
    Customer customer = new Customer();
    customer.setName("Will Smith");
    customer.setAddress("100 Main Street");
    customer.setState("California");
    customer.setZip("10011");
    myDataMap.put(Associate.class, associate);
    myDataMap.put(Customer.class, customer);
  
    System.out.println("\n---[MutableClassToInstanceMap with Objects]-----");
    System.out.println("Value for Customer.class is: " + myDataMap.get(Customer.class));
    System.out.println("Value for Associate.class is: " + myDataMap.get(Associate.class));   
  }
}

That’s It!

I hope you enjoyed this tutorial. Please continue to share the love and like us so that we can continue bringing you quality tutorials. Happy Coding!!!

GuavaClassToInstanceMap

Related Posts

Please Share Us on Social Media

Facebooktwitterredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *